I’ve been struggling with this for a while, and decided to come to here and seek help.
Basically, I am using this code I wrote with the help of a tutorial (modified to suit my needs) but I can’t quite figure out a way to have it equip if the player clicks a button (E, Z, 1, etc.). I have attached the local code as well as the module script below, if anyone wants to take a crack at it. Thanks!
Local Script:
local GunModel = game.ReplicatedStorage:WaitForChild("FN Scar")
local Viewmodel = game.ReplicatedStorage:WaitForChild("Viewmodel")
local AnimFolder = game.ReplicatedStorage:WaitForChild("ScarAnimations")
local mainModule = require(game.ReplicatedStorage.MainModule)
local SpringModule = require(game.ReplicatedStorage.SpringModule)
local IsPlayerHoldingMouse
local CanFire = true
local Delay = 0.1
local RecoilSpring = SpringModule.new()
local BobbleSpring = SpringModule.new()
local SwayingSpring = SpringModule.new()
local Fire = game.ReplicatedStorage:WaitForChild("Fire")
local ammoval = game.ReplicatedStorage:WaitForChild("Values"):WaitForChild("AmmoAmount")
local AmmoAmount = 30
ammoval.Value = AmmoAmount
Viewmodel.Parent = game.Workspace.Camera
mainModule.weldgun(GunModel)
mainModule.equip(Viewmodel, GunModel, AnimFolder.Hold)
game:GetService("RunService").RenderStepped:Connect(function(dt)
mainModule.update(Viewmodel, dt, RecoilSpring, BobbleSpring, SwayingSpring, GunModel)
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = true
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
mainModule.aim(true, Viewmodel, GunModel)
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = false
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
mainModule.aim(false, Viewmodel, GunModel)
elseif input.KeyCode == Enum.KeyCode.R then
print(game.Players.LocalPlayer.Name.." reloaded")
AmmoAmount = mainModule.reload(Viewmodel, AnimFolder.Reload, 30)
end
end)
game:GetService("RunService").Heartbeat:Connect(function(dt)
if IsPlayerHoldingMouse then
if CanFire and AmmoAmount > 0 then
AmmoAmount -= 1
ammoval.Value -= 1
CanFire = false
RecoilSpring:shove(Vector3.new(5, 2, 3))
coroutine.wrap(function()
local FireSound = GunModel.GunComponents.Sounds.Fire:Clone()
FireSound.Parent = game.Workspace
FireSound.Parent = nil
FireSound:Destroy()
end)()
coroutine.wrap(function()
wait(0.2)
RecoilSpring:shove(Vector3.new(-2.8, math.random(-4, 4), -10))
end)()
local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.FilterDescendantsInstances = {Viewmodel, game.Players.LocalPlayer.Character}
local Mouse = mainModule.GetMouse(1000, CastParams)
mainModule.cast(GunModel.GunComponents.Barrel.Position, Mouse, 5, game.ReplicatedStorage.Damage)
Fire:FireServer(GunModel.GunComponents.Barrel.Position, Mouse)
wait(0.2)
CanFire = true
end
end
end)
Fire.OnClientEvent:Connect(function(client, origin, endposition)
if client ~= game.Players.LocalPlayer then
mainModule.cast(origin, endposition, 5, nil)
end
end)
Module Script:
local module = {}
local function GetBobbing(addition)
return math.sin(tick() * addition * 1.3) * 0.8
end
function module.update(viewmodel, dt, RecoilSpring, BobbleSpring, SwayingSpring, gun)
viewmodel.HumanoidRootPart.CFrame = game.Workspace.Camera.CFrame
local MouseDelta = game:GetService("UserInputService"):GetMouseDelta()
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local UpdatedRecoilSpring = RecoilSpring:update(dt)
local UpdatedSwaySpring = SwayingSpring:update(dt)
gun.GunComponents.Sight.CFrame = gun.GunComponents.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame, game.ReplicatedStorage.Values.AimAlpha.Value)
viewmodel.HumanoidRootPart.CFrame *= CFrame.new(UpdatedSwaySpring.X, UpdatedSwaySpring.Y, 0)
viewmodel.HumanoidRootPart.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X) * 2, 0, 0)
game.Workspace.Camera.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X), math.rad(UpdatedRecoilSpring.Y), math.rad(UpdatedRecoilSpring.Z))
end
function module.weldgun(gun)
local Main = gun.GunComponents.Handle
for i, v in ipairs(gun:GetDescendants()) do
if v:IsA("BasePart") and v ~= Main then
local newMotor = Instance.new("Motor6D")
newMotor.Name = v.Name
newMotor.Part0 = Main
newMotor.Part1 = v
newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
newMotor.Parent = Main
end
end
end
function module.equip(viewmodel, gun, hold)
local gunHandle = gun.GunComponents.Handle
local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle
gun.Parent = viewmodel
HRP_Motor6D.Part1 = gunHandle
local Hold = viewmodel.AnimationController:LoadAnimation(hold)
Hold:Play()
end
function module.cast(origin, endposition, velocity, damage)
local Bullet = Instance.new("Part")
Bullet.Name = "Bullet"
Bullet.Parent = workspace.Bullets
Bullet.Size = Vector3.new(1,1,3)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Color = Color3.new(0.933333, 0.803922, 0.0117647)
Bullet.Material = Enum.Material.Metal
Bullet.CFrame = CFrame.new(origin, endposition)
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
if (Bullet.Position - origin).magnitude > 5000 then
Bullet:Destroy()
Loop:Disconnect()
end
local Hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector * velocity * 1.5)
if Hit then
if Hit.Instance.Parent:FindFirstChild("Humanoid") and damage ~= nil then
game.ReplicatedStorage:WaitForChild("Damage"):FireServer(Hit.Instance.Parent, 10)
print("bullet from "..game.Players.LocalPlayer.Name.." hit a player i guess")
else
print("bullet hit a wall")
Loop:Disconnect()
Bullet:Destroy()
end
end
end)
end
function module.aim(toaim, viewmodel, gun)
if toaim then
game:GetService("TweenService"):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(0.5), {Value = 1}):Play()
else
game:GetService("TweenService"):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(0.5), {Value = 0}):Play()
end
end
function module.GetMouse(Distance, CastParams)
local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
local UnitRay = game:GetService("Workspace").Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y)
local origin = UnitRay.Origin
local endp = UnitRay.Direction * Distance
local Hit = game:GetService("Workspace"):Raycast(origin, endp, CastParams)
if Hit then
return Hit.Position
else
return UnitRay.Origin + UnitRay.Direction * Distance
end
end
function module.reload(viewmodel, reloadAnim, ammoamt)
print("module recieved request to reload")
local reload = viewmodel.AnimationController:LoadAnimation(reloadAnim)
reload.Looped = false
reload:Play()
reload.Stopped:Wait()
print("reloaded")
game.ReplicatedStorage:WaitForChild("Values").AmmoAmount.Value = ammoamt
return ammoamt
end
return module