Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local runService = game:GetService("RunService")
- local replicatedStorage = game:GetService("ReplicatedStorage")
- local players = game:GetService("Players")
- local UserInputService = game:GetService("UserInputService")
- local debris = game:GetService("Debris")
- local player = players.LocalPlayer
- local viewModels = workspace:WaitForChild("ViewModels")
- local camera = workspace.CurrentCamera
- local mouse = player:GetMouse()
- local remotes = replicatedStorage.Remotes
- local guns = replicatedStorage:WaitForChild("Guns")
- local gunConnection = nil
- local currentAnimation = nil
- local viewModel = nil
- local gun = nil
- local aiming = false
- local firingWeapon = false
- local WeaponEquipped = false
- local FireDebounce = false
- local configuration = nil
- local lastFireTick = tick()
- local module = {}
- local function Fire()
- if (tick() - lastFireTick) >= configuration.Debounce then
- remotes.Fire:FireServer(mouse.Target, mouse.Hit, configuration, gun)
- lastFireTick = tick()
- else
- return
- end
- end
- local function PositionViewModel(gun: Model)
- local gunOffset = configuration.Offset
- local lastCameraCFrame = camera.CFrame
- local targetCFrame = CFrame.new()
- local swayOffset = CFrame.new()
- local swaySize = 1
- local isWalking = false
- gunConnection = runService.RenderStepped:Connect(function(deltaTime)
- local character = player.Character or player.CharacterAdded:Wait()
- if character then
- local humanoid = character:FindFirstChild("Humanoid")
- if humanoid then
- if humanoid.MoveDirection.Magnitude > 0 then
- isWalking = true
- else
- isWalking = false
- end
- local aimOffset = gun.Aim.CFrame:Inverse() * camera.CFrame * CFrame.new(0, gunOffset.Y, 0)
- local walkSineWave = math.sin(time() * 2*4) / 25
- local walkCosWave = math.cos(time() * 3*4) / 20
- local walkSineRotation = math.sin(time() * 2*4) /25
- local walkCosRotation = math.cos(time() * 3*4) / 20
- local X, Y, Z = (camera.CFrame:ToObjectSpace(lastCameraCFrame)):ToOrientation()
- swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(X / 2) * swaySize, math.sin(Y / 2) * swaySize, 0), 0.1)
- local walkCFrameOffset = CFrame.new(walkSineWave, walkCosWave, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSineRotation)
- if aiming then
- walkCFrameOffset = CFrame.new(walkSineWave / 10, walkCosWave / 10, 0)
- end
- if isWalking and not aiming then
- targetCFrame = targetCFrame:Lerp(gunOffset * walkCFrameOffset * swayOffset, 0.1)
- elseif not isWalking and not aiming then
- targetCFrame = targetCFrame:Lerp(gunOffset * swayOffset, 0.1)
- end
- if isWalking and aiming then
- targetCFrame = targetCFrame:Lerp(walkCFrameOffset * swayOffset * aimOffset, 0.1)
- elseif not isWalking and aiming then
- targetCFrame = targetCFrame:Lerp(swayOffset * aimOffset, 0.1)
- end
- lastCameraCFrame = camera.CFrame
- gun:PivotTo(camera.CFrame * targetCFrame)
- if firingWeapon and configuration.Automatic then
- Fire()
- end
- end
- end
- end)
- end
- function module.EquipGun(gunName: string)
- local foundGun = guns:FindFirstChild(gunName)
- if foundGun == nil then
- print("GUN_NOT_FOUND")
- return
- end
- gun = foundGun:Clone()
- gun.Parent = viewModels
- configuration = require(foundGun.config)
- PositionViewModel(gun)
- viewModel = gun
- local character = player.Character or player.CharacterAdded:Wait()
- if character == nil then return end
- local humanoid = character:FindFirstChild("Humanoid")
- if humanoid == nil then return end
- local animator = humanoid:FindFirstChild("Animator")
- if animator == nil then return end
- if currentAnimation ~= nil then
- currentAnimation:Stop()
- end
- currentAnimation = animator:LoadAnimation(foundGun.Animations.Idle)
- currentAnimation:Play()
- WeaponEquipped = true
- end
- function module.ClearAllItems()
- if currentAnimation ~= nil then
- currentAnimation:Stop()
- end
- if gunConnection ~= nil then
- gunConnection:Disconnect()
- viewModels:ClearAllChildren()
- end
- WeaponEquipped = false
- configuration = nil
- viewModel = nil
- end
- UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
- if gameProcessedEvent and not WeaponEquipped then return end
- if input.UserInputType == Enum.UserInputType.MouseButton2 then
- aiming = true
- end
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- firingWeapon = true
- end
- end)
- UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
- if gameProcessedEvent and not WeaponEquipped then return end
- if input.UserInputType == Enum.UserInputType.MouseButton2 then
- aiming = false
- end
- if input.UserInputType == Enum.UserInputType.MouseButton1 then
- firingWeapon = false
- end
- end)
- mouse.Button1Up:Connect(function()
- if configuration ~= nil and not configuration.Automatic then
- Fire()
- end
- end)
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement