Advertisement
ScriptKid996

Submission

Jan 9th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local runService = game:GetService("RunService")
  2. local replicatedStorage = game:GetService("ReplicatedStorage")
  3. local players = game:GetService("Players")
  4. local UserInputService = game:GetService("UserInputService")
  5. local debris = game:GetService("Debris")
  6.  
  7. local player = players.LocalPlayer
  8. local viewModels = workspace:WaitForChild("ViewModels")
  9. local camera = workspace.CurrentCamera
  10. local mouse = player:GetMouse()
  11. local remotes = replicatedStorage.Remotes
  12.  
  13. local guns = replicatedStorage:WaitForChild("Guns")
  14.  
  15. local gunConnection = nil
  16. local currentAnimation = nil
  17. local viewModel = nil
  18. local gun = nil
  19.  
  20. local aiming = false
  21. local firingWeapon = false
  22. local WeaponEquipped = false
  23. local FireDebounce = false
  24.  
  25. local configuration = nil
  26.  
  27. local lastFireTick = tick()
  28.  
  29. local module = {}
  30.  
  31.  
  32.  
  33. local function Fire()
  34.     if (tick() - lastFireTick) >= configuration.Debounce then
  35.         remotes.Fire:FireServer(mouse.Target, mouse.Hit, configuration, gun)
  36.         lastFireTick = tick()
  37.     else
  38.         return
  39.     end
  40. end
  41.  
  42. local function PositionViewModel(gun: Model)
  43.     local gunOffset = configuration.Offset
  44.     local lastCameraCFrame = camera.CFrame
  45.     local targetCFrame = CFrame.new()
  46.     local swayOffset = CFrame.new()
  47.     local swaySize = 1
  48.     local isWalking = false
  49.        
  50.     gunConnection = runService.RenderStepped:Connect(function(deltaTime)
  51.         local character = player.Character or player.CharacterAdded:Wait()
  52.         if character then
  53.             local humanoid = character:FindFirstChild("Humanoid")
  54.             if humanoid then
  55.                 if humanoid.MoveDirection.Magnitude > 0 then
  56.                     isWalking = true
  57.                 else
  58.                     isWalking = false
  59.                 end
  60.                
  61.                 local aimOffset = gun.Aim.CFrame:Inverse() * camera.CFrame * CFrame.new(0, gunOffset.Y, 0)
  62.                 local walkSineWave = math.sin(time() * 2*4) / 25
  63.                 local walkCosWave = math.cos(time() * 3*4) / 20
  64.                 local walkSineRotation = math.sin(time() * 2*4) /25
  65.                 local walkCosRotation = math.cos(time() * 3*4) / 20
  66.                
  67.                 local X, Y, Z = (camera.CFrame:ToObjectSpace(lastCameraCFrame)):ToOrientation()
  68.                 swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(X / 2) * swaySize, math.sin(Y / 2) * swaySize, 0), 0.1)
  69.                
  70.                 local walkCFrameOffset = CFrame.new(walkSineWave, walkCosWave, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSineRotation)
  71.                
  72.                 if aiming then
  73.                     walkCFrameOffset = CFrame.new(walkSineWave / 10, walkCosWave / 10, 0)
  74.                 end
  75.                
  76.                 if isWalking and not aiming then
  77.                     targetCFrame = targetCFrame:Lerp(gunOffset * walkCFrameOffset * swayOffset, 0.1)
  78.                 elseif not isWalking and not aiming then
  79.                     targetCFrame = targetCFrame:Lerp(gunOffset * swayOffset, 0.1)
  80.                 end
  81.                
  82.                 if isWalking and aiming then
  83.                     targetCFrame = targetCFrame:Lerp(walkCFrameOffset * swayOffset * aimOffset, 0.1)
  84.                 elseif not isWalking and aiming then
  85.                     targetCFrame = targetCFrame:Lerp(swayOffset * aimOffset, 0.1)
  86.                 end
  87.                
  88.                 lastCameraCFrame = camera.CFrame
  89.                
  90.                 gun:PivotTo(camera.CFrame * targetCFrame)
  91.                 if firingWeapon and configuration.Automatic then
  92.                     Fire()
  93.                 end
  94.             end
  95.         end
  96.     end)
  97. end
  98.  
  99. function module.EquipGun(gunName: string)
  100.     local foundGun = guns:FindFirstChild(gunName)
  101.    
  102.     if foundGun == nil then
  103.         print("GUN_NOT_FOUND")
  104.         return
  105.     end
  106.    
  107.     gun = foundGun:Clone()
  108.     gun.Parent = viewModels
  109.    
  110.     configuration = require(foundGun.config)
  111.     PositionViewModel(gun)
  112.     viewModel = gun
  113.    
  114.     local character = player.Character or player.CharacterAdded:Wait()
  115.     if character == nil then return end
  116.     local humanoid = character:FindFirstChild("Humanoid")
  117.     if humanoid == nil then return end
  118.     local animator = humanoid:FindFirstChild("Animator")
  119.     if animator == nil then return end
  120.     if currentAnimation ~= nil then
  121.         currentAnimation:Stop()
  122.     end
  123.     currentAnimation = animator:LoadAnimation(foundGun.Animations.Idle)
  124.     currentAnimation:Play()
  125.     WeaponEquipped = true
  126. end
  127.  
  128. function module.ClearAllItems()
  129.     if currentAnimation ~= nil then
  130.         currentAnimation:Stop()
  131.     end
  132.     if gunConnection ~= nil then
  133.         gunConnection:Disconnect()
  134.         viewModels:ClearAllChildren()
  135.     end
  136.     WeaponEquipped = false
  137.     configuration = nil
  138.     viewModel = nil
  139. end
  140.  
  141. UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
  142.     if gameProcessedEvent  and not WeaponEquipped then return end
  143.    
  144.     if input.UserInputType == Enum.UserInputType.MouseButton2 then
  145.         aiming = true
  146.     end
  147.    
  148.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  149.         firingWeapon = true
  150.     end
  151. end)
  152.  
  153. UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
  154.     if gameProcessedEvent and not WeaponEquipped then return end
  155.    
  156.     if input.UserInputType == Enum.UserInputType.MouseButton2 then
  157.         aiming = false
  158.     end
  159.    
  160.     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  161.         firingWeapon = false
  162.     end
  163. end)
  164.  
  165. mouse.Button1Up:Connect(function()
  166.     if configuration ~= nil and not configuration.Automatic then
  167.         Fire()
  168.     end
  169. end)
  170.  
  171. return module
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement