Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I stop my raycast gun from having a lag delay?

Asked by
skyaz1 72
4 years ago

Hey, I've been having a problem with a gun I'm making with raycasts. I'm making guns for a Star Wars group. The gun works fine, it creates a beam and all of that, however, there is a delay between when the gun shoots and when the beam actually shows up.

https://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/3/1/3/313c0ede57e3ff008c4c1724ab3bd72b1d4a5f7c.gif

The link is to a gif of what I mean. It's not actually me in that gif, but it shows what the problem is with my gun. The beam shows up a fraction of a second late, and that causes it to look like it's coming from thin air. Is there any way to stop this from happening?

1 answer

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

this is probably because of client-server latency

there's a little bit of delay between actions happening on the server and actions happening on the client, so when the client tells the server to fire the gun and make the beam, there's a delay, and then when the server creates the beam and tells the client "hey i made the beam", there's another delay

what people usually do to get around this is to create a "fake" beam on the original client, and then have the server do tell all the other clients to create a beam as well once the gun is fired, so that other clients see the beam but the original client gets instant feedback

while all of this is happening all of the damage calculations, raycasting, all that sort of stuff is still on the server, it's just "invisible" in a way

you could make a "fake" beam on the original client, then just make one on the server, but then you have to do a whole bunch of hacky stuff to destroy that beam on the original client and it's just generally Not Good so i prefer doing it on every client individually

-- client
local CreateBeamRemote = path.to.RemoteEvent -- for the beam effect on the client
local FireGunRemote = path.to.AnotherRemoteEvent

local function CreateBeam(origin, endpoint)
    -- idk do some beam making stuff here
end

local function FireGun()
    local origin = whatever
    local endpoint = wateva
    CreateBeam(origin, endpoint)
    FireGunRemote:FireServer(origin, endpoint, idkSomethingImportantProbablyGoesHere)
end

CreateBeamRemote.OnClientEvent:Connect(CreateBeam) -- we can just connect this to the CreateBeam function so that we don't need to write the code for making beams in two places

-- server
local Players = game:GetService("Players")

local CreateBeamRemote = path.to.RemoteEvent
local FireGunRemote = path.to.AnotherRemoteEvent

FireGunRemote.OnServerEvent:Connect(function(player, origin, endpoint, idkotherstuff)
    -- sanity checks blablabla
    for _, playerToSend in pairs(Players:GetPlayers()) do
        if playerToSend ~= player then -- of course we don't want the player who originally fired making a second beam, so we just check if the player we're firing the beam remote to isn't the original player
            CreateBeamRemote:FireClient(playerToSend, origin, endpoint)
        end
    end
    -- put hit detection here probably
end)

of course you don't need to use this exact code, it's just a suggestion...also if you used this exact code it would probably be very broken

0
Thanks! This really cleared things up. skyaz1 72 — 4y
Ad

Answer this question