FireClient(Player) don't work randomly

Hello, I’ve already used FireClient(player) hundreds of times.

I don’t understand why randomly or in certain scripts it doesn’t work while the other function, which is a copy and paste, does.

For example, this code
Localscript :

while task.wait(1) do
	if text then
		RequestSablierEx:FireServer()
		text.Text = IntValue.Value
	end
end

RequestSablierEx.OnClientEvent:Connect(function(RECEIVEDINT)
	IntValue.Value = RECEIVEDINT
end)

Server Script :

RequestTix.OnServerEvent:Connect(function(player)
	local success, int = pcall(function()
		return CurrencyTix:GetAsync(player.UserId)
	end)
	if success and int then
		RequestTix:FireClient(player, int)
	end
end)

Client → Server OK
Server → Client NOT OK
Player is not nil, Int is not nil, ALL IS VALID

And this code work :
localscript :

button.Activated:Connect(function()
	RequestBooster:FireServer(ID, Price)
end)

RequestBooster.OnClientEvent:Connect(function(ID, isGiven)
	if isGiven then
		local UIToClone = game:GetService("ReplicatedStorage").Ressource.UI.Booster.ShowBooster:Clone()
		UIToClone.Parent = game:GetService("Players").LocalPlayer.PlayerGui
		UIToClone.IDBooster.Value = ID
		
		
		print("Booster given")
	else
		print("You don't have enough money!")
	end
end)

Server script :

RequestBooster.OnServerEvent:Connect(function(player, ID)
	local localSablier = GetSablier(player)
	
	if localSablier >= Price then
		localSablier = localSablier - Price
		local success, errorMessage = pcall(function()
			inventoryStore:SetAsync(player.UserId, localSablier)
		end)
		if not success then
			warn("Erreur lors de la sauvegarde de l'inventaire pour :", player.Name, errorMessage)
		else
			RequestBooster:FireClient(player, ID, true)
		end
	else
		RequestBooster:FireClient(player, ID, false)
	end
	
end)

Client → Server OK
Server → Client OK
Player is not nil, Int is not nil, ALL IS VALID

Are you sure CurrencyTix:GetAsync is returning success in those failure cases?

1 Like

Normally, code is executed from top → bottom, left → right. When code execution reaches the bottom of a loop, the loop modifies this natural flow by returning code execution to the top and repeating this pattern, so long as its condition remains true. This means that code below a loop will not be executed until the loop breaks. Your loop in particular is an infinite loop, as its condition can never be falsy

FireClient isn’t the problem. It’s the fact that you’re never able to connect a callback to OnClientEvent

2 Likes

Yep, I overlooked that. Ziffix is right, it’s never getting outside of your loop to setup the connection.

1 Like

You just need to move the connection above the loop, because the loop yields the script which prevents all the code below from running.

RequestSablierEx.OnClientEvent:Connect(function(RECEIVEDINT)
	IntValue.Value = RECEIVEDINT
end)

while task.wait(1) do
	if text then
		RequestSablierEx:FireServer()
		text.Text = IntValue.Value
	end
end
1 Like

The while Condition do loop is what is causing your problem. You need to place it after you connect to the RequestSablierEx on client event. The reason is because, the code is run from top to bottom, and it never reaches the event connection, because it has entered into and is running an infinite loop with no end before you have connected to the event. Here is a script that would probably work:

RequestSablierEx.OnClientEvent:Connect(function(RECEIVEDINT)
	IntValue.Value = RECEIVEDINT
end)
while task.wait(1) do
	if text then
		RequestSablierEx:FireServer()
		text.Text = IntValue.Value
	end
end

Or, if you need to run the while loop before you connect to the event for whatever reason, you can use task.spawn:

task.spawn(function()
    while task.wait(1) do
    	if text then
		    RequestSablierEx:FireServer()
		    text.Text = IntValue.Value
	    end
    end
end)
RequestSablierEx.OnClientEvent:Connect(function(RECEIVEDINT)
	IntValue.Value = RECEIVEDINT
end)

Edit: @Crygen54 beat me to it.

1 Like

I had completely forgotten that Lua doesn’t work the same way as C++ & C#.

I get it wrong every time :sweat_smile:

Thank you so much !

Thanks for all the details of how it works, sorry I’m too used to the logic of C++ sometimes I get it wrong because of Lua haha

1 Like

Thank you so much ! :+1:
Solution found thanks to you!

1 Like

Already tested :wink:
Is not this but logic func