Gun firing late when clicked ingame but works fine in studio

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.

2 Likes

maybe because it waits “FireRate” ?

1 Like

This is so you can’t shoot without any debounce, but he should use task.wait() as it’s more precise

1 Like

its not the issue, the gun seems to wait a second before event sending the signals, its almost like my clicks are not getting there in time

nope that didnt fix it, i use remote events on the .triggered event though, could that have a effect?

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.

4 Likes

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.

1 Like

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.

1 Like

how would i make it so the other players also see the visuals without delay or is that not possible?

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.

maybe, ill try that and see if It works, thanks