Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to I make my first person gun script snappier?

SCRIPT:

local player = game.Players.LocalPlayer
local char = player.Character
local cam = workspace.Camera
local arms = game.ReplicatedFirst.Arms:Clone()
local MyGun = script.Parent
local GunHandle = MyGun:WaitForChild("Handle")
arms.Parent = cam
local Humanoid = arms.Humanoid
local IdleAnimation = script.IdleAnim
local ShootingAnimation = script.ShootAnim
local ShootAnimTrack = Humanoid:LoadAnimation(ShootingAnimation)
local IdleAnimTrack = Humanoid:LoadAnimation(IdleAnimation)
IdleAnimTrack:Play()
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	ShootAnimTrack:Play()
	game.ReplicatedStorage.Shoot:FireServer(mouse.Hit.p)
end)

game:GetService("RunService").Stepped:Connect(function()
	for i = 1, 100 do
		arms:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,-2,-2.5) * CFrame.Angles(0,0,0))
	end
end)	

I am trying to make a gun script and it works, but the issue is that when you turn the camera, it looks clunky and it stutters. Is there any way I can make it more snappy?

2 comments

1

Just out of curiosity, have you tried using RenderStepped instead of Stepped?

1

Woah, that actually worked. If you want, you can make an answer so that I can give you and I karma. Thanks! :)

2 answers

✓ Accepted answer

Try using RenderStepped instead of Stepped.

game:GetService("RunService").RenderStepped:Connect(function()
    for i = 1, 100 do
        arms:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,-2,-2.5) * CFrame.Angles(0,0,0))
    end
end)    

I already found out this solution due to Gey4Jesus69. When using run service, use RenderStepped instead of Stepped, it's more reliable.

3 comments

1

Sorry, I didn't care to try to explain the reason why it worked, because I know very little about it myself. According to the wiki, RenderStepped fires every frame prior to the frame being rendered. However, Stepped fires every frame prior to the physics simulation. I assume Stepped probably avoids actual latency issues, whereas RenderStepped is more beneficial for anything regarding framerate

1

That is why I suggested RenderStepped, because your issue isn't actually focused on physics in-game, rather the way your movement looks

0

Yeah, that makes sense. Thank you for the help and I also learned something new!

This discussion is preserved as it was. New answers, comments, and votes are closed.