FireClient event occurring before server finished function

I have added a server script that runs a function whenever a character is added for a player, so it can add random accessories to their character. After the accessories are added, the server fires an event to the client, indicating that the server has added all required accessories, so the client can do stuff with them.

The client function that picks up the event is firing BEFORE the server finishes adding the accessories. Genuinely have no idea what’s going on here.
And to add, this only occurs in an actual Roblox server, the issue isn’t present in Studio.

Here is a snippet of the SERVER code looks like:

local function OnPlayerAdded(p)
	p.CharacterAdded:Connect(function(char)
		local passed = OnCharacterAdded(char) -- Function that adds accessories
		print("server pass: " .. tick()) -- Prints the Tick when the adding of accessories has finished
		if passed then
			characterLoadedEvent:FireClient(p) -- Fire an event to the client
		end
	end)
end

Here is a snippet of what the CLIENT code looks like:

characterLoadedEvent.OnClientEvent:Connect(function()
	print("client pass: " .. tick()) -- Prints the Tick when the client picks up the event fired from the server
end)

In Studio, the server tick is lower than the client (as it should).
In an actual Roblox server, the client tick is lower than the server (which makes no sense).

Results are shown below:


Am I missing something obvious, or is there a solution to this?

1 Like

The server and the device could be not in perfect sync about the tick? cause the tick readed in server could be different to the tick readed in client?

I think its not possible the client get the signal before the server fired it.
So comparing the tick visually when did happen in time in server vs the client, would be not useful to read the real order in which the events did happen

Another thing, how looks the OnCharacterAdded(char) function it returns true when finishing? not spawn() anything into it?

It just returns whether it could actually carry out the process without any errors or not.

Turns out the problem isn’t to do with the FireClient event. It is somewhere else in my code.
For some reason, In an actual Roblox server, Sometimes the client doesn’t recognise the accessories being added.

You misunderstand your problem. All your code is sequential so FireClient is still only going to be called after OnCharacterAdded finishes executing, the print is called and the if statement passes.

The problem here is different machines, different ticks. You don’t see this in Studio testing because your machine is used as the host for the server for a local test as well. In a live server, your client is connecting to a server that isn’t your machine in a different region (and time zone).

Use workspace:GetServerTimeNow() instead. It has high precision and it’s reliant on pulling the time from the server. It’s good mostly for synchronisation between clients and the server but in your case it would just show the client event arriving after the server fires.

That being said, given the instance of your code running sequentially, this essentially was an issue of putting the cart before the horse. You probably should’ve built out your features first rather than checking the timing first.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.