Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Best practice for Server to Client communication?

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?

2 comments

0

are you having issues with latency or performance?

0

not necessarily, but in case if players do, and if there are any better practices to achieve this in general

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.