Laggy Gun RayCast

Hello!
I made a gun system by using ray casting, but I have one issue - is lag!!
https://gyazo.com/149f982b66238141e9207c02b31bb617

So there how the gun lags, the place isn’t full of stuff, just a gun.

As you can see the ray cast is delaying, I mean when a player clicks the left mouse button, after 0.1s the ray comes out.

If you know the solution, help.

Note: I didn’t put wait in the ray cast code.

Um, but the ray remote gets fired by fire function.

This is another case of “laggy cannons”. The server obviously have some kind of small lag. Clients are fast enough to send requests to server, however the server obviously have some issue with receiving the input “in time”.

https://developer.roblox.com/en-us/articles/Fighting-Against-Lag


I’m having some limitations of internet and I have no access to Studio at the moment. Fighting the lag takes some gruesome effort to synchronize the occurrences between client and server.

1 Like

Ok, I am not entirely sure this is the correct answer, but you’re using RemoteEvents, which means you have to send all the data to replicate it on the server and you are sending quite a few parameters. I think that delay you notice is that data being sent to the server, try running it locally and see if it lags.

That’s unsafe, considering how the client can abuse this if they were exploiting. They could easily get away with damaging another player despite being across the map, behind a wall or generally unseen.

So, what do I have to do?? :neutral_face:

I might have misinterpreted your reply there. The end product of the code would look like spaghetti and you’ll probably have to divide the code into multiple functions.

I believe I’ve thought about that model of yours. I should test that when I have finished my FastCast weapon testing and stuff.

Not necessarily. Both the client and the server are capable of doing raycasts or you can leave the server in charge of verification of hits sent from the client. The point is to minimise discrepancies and have both environments performing the same relative function. Multiple functions doesn’t mean spaghetti code.

3 Likes

So have you found the fix? I have a similar problem and my code is very lackluster, just a simple localscript attached to a script and a remoteevent

Localscript:

script.Parent.Equipped:Connect(function(mou)
	mou.Button1Down:Connect(function()
		script.Parent.Script.RemoteEvent:FireServer(mou.Hit,mou.Target)
	end)
end)

Script:

script.RemoteEvent.OnServerEvent:Connect(function(p,e,r)

local beam = Instance.new("Part",workspace)

beam.Material = "Neon"

beam.Anchored = true

beam.CanCollide = false

beam.Transparency = .8

beam.BrickColor=BrickColor.new('White')

beam.Size=Vector3.new(.05,.05,(script.Parent.Shoot.Position-e.p).Magnitude)

beam.CFrame=CFrame.new((script.Parent.Shoot.Position+e.p)/2,e.p)

game:GetService("Debris"):AddItem(beam,0.03)

script.Parent["Beretta Pistol Sound"]:Play()

end)