hello there, so I have this issue currently where my gun fires as soon as the player clicks in studio but when I play the game from the site, the gun waits like 0.5-1 second before actually firing the shot, I was wondering if there is a fix for this? I know other people have had this problem but none of the solutions I have seen have applied to me, any help is appreciated, thanks.
Part of the code that sends the signal to fire:
script.Parent.Activated:Connect(function(Fire)
if CanFire == true then
CanFire = false
local RawTarg = PlayerMouse.Hit
local RayCastOrigin = script.Parent.FirePoint.Position
local RayCastDirection = (RawTarg.Position - RayCastOrigin).Unit
local raycastResult = workspace:Raycast(RayCastOrigin, RayCastDirection*script.Parent:GetAttribute("Range"), RayCastPerm)
if raycastResult then
script.Parent.FireEvent:FireServer(raycastResult.Position, raycastResult.Instance, raycastResult.Normal)
--[[ TEST PRINTS
print("------------------------------------------------")
print("Instance:", raycastResult.Instance)
print("Position:", raycastResult.Position)
print("Distance:", raycastResult.Distance)
print("Material:", raycastResult.Material)
print("Normal:", raycastResult.Normal)
print("Name:", raycastResult.Instance.Name )
print("------------------------------------------------")
--]]
end
if not raycastResult then
script.Parent.EmptyFire:FireServer()
end
wait(FireRate)
CanFire = true
end
end)
if more parts of the code are needed please do tell, the entire thing is too large if I send both server and client side.
This is due to latency between the client and the server, there’s nothing you can do about that. The only solution I can think of is having the visuals of the bullet be on the client so there would be no delay on it, but have all the damage stuff on the server.
Add a print on the client right before the remote event fire. You’ll see if the client is slow, or if it’s the server. If the client prints first, then the server is late, then it’s latency.
If you are expecting the gun to fire quickly on the server, there will be a slight delay. However, if you do it on client side, it will process much quicker. You have to remember that the player’s ping + Roblox server will add delay for things to process.
For that you would fire a remote to the server that then fires a remote to all clients to render it. Since players already experience replication lag when looking at another player, the firing would be in sync with the movements they see.