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

Best practice for Server to Client communication?

Asked by 5 years ago

In my game, im planning to have it so that when players click a door, they teleport onto the other side.

It works by clicking on the door, then a RemoteEvent is fired to the client, which on the client side a transition animation occurs and also teleports the client which is also handled by the Client.

Server Side

local transitioning = {}
local Events = game.ReplicatedStorage.Events
Model.DoorIn.ClickDetector.MouseClick:Connect(function(Plr)
    if not transitioning[Plr] then -- so that the player cannot request another transition whilst a transitioning is already happening.
        Events.Transition:FireClient(Plr, Model.TeleportIn.CFrame)
        transitioning[Plr] = true
        print(Plr)
        wait(0.6)
        transitioning[Plr] = nil
    end
end)

Client Side

Events.Transition.OnClientEvent:Connect(function(returnedData)
    transitionIn() -- does the GUI transition animation
    wait(0.5)
    Plr.Character:SetPrimaryPartCFrame(returnedData)
    transitionOut()
end)

Is there a better way to do this? More on the side of better performance and compensating for latency delay?

0
are you having issues with latency or performance? DinozCreates 1070 — 5y
0
not necessarily, but in case if players do, and if there are any better practices to achieve this in general Marmalados 193 — 5y

1 answer

Log in to vote
0
Answered by
pidgey 548 Moderation Voter
5 years ago

If you're just handling the local player, do everything in a localscript. Server-side is redundant for that. An exploiter can set the position of their Character to anything, anyway.

Also, ClickDetector allows both Scripts and LocalScripts to receive user input.

Model.DoorIn.ClickDetector.MouseClick:Connect(function(Player)
    Player.Character:SetPrimaryPartCFrame(Model.TeleportIn.CFrame)
end)

If you want a debounce handler, just fire a remotefunction and decide to move the character or not client-side with a simple sentinel boolean value. You don't need to know every player transitioning unless you're doing something with that data. Otherwise, we can simplify.

Server

myDebounce = game.ReplicatedStorage.RemoteFunction
local debounce = false
myDebounce.OnServerInvoke = function(Plr)
    if debounce == false then
        spawn(function() --your function to handle the debounce instruction in another thread
            debounce = true
            wait(0.5)
            debounce = false
        end)

        return true --tell the client its safe to tp
    end

    return false --its not safe to tp
end

Client

myDebounce = game.ReplicatedStorage.RemoteFunction
Model.DoorIn.ClickDetector.MouseClick:Connect(function(Player)
    if myDebounce:InvokeServer() == true then
        Player.Character:SetPrimaryPartCFrame(Model.TeleportIn.CFrame)
    end
end)
Ad

Answer this question