Remote event firing based on # of players in game

So the problem is pretty evident in the title. I’ve got a remote event and say there are four people in-game, that event should only fire once fires four times (or the equivalent of the # of players in-game.), and this happens dynamically with any number of players.

I’ve checked over my code over 20 times, and that’s not an over-exaggeration; there is positively nothing anywhere that could be causing this to happen but Roblox itself, and I want a fix for it.

That said, if you guys need any details about the location of the script etc. ill be glad to provide but I checked to make sure that wasn’t the issue either.

1 Like

You should include the code so it is possible for us to help.

i mean the code is in multiple scripts and its kind of long. is there any specific parts that you would like me to send?

This would be the code to fire an event for the anmount of players in the server as an example, if thats what you mean:


local plrs = game.Players:GetPlayers()

for i = #plrs, 0, -1 do
    RemoteEvent:FireServer()
end


Combine this with an event fired on .PlayerAdded or .PlayerLeaving if you want it to change when people join/leave

This is pretty straight forward, you have a LocalScript which fires the RemoteEvent once it runs. But that LocalScript will run for each single player therefore firing the remote #players amount of times. If you want the remote to only fire once after it’s fired for the first time, disconnect the server event(that way if it’s fired again the server script will ignore it):

local Remote = game.ReplicatedStorage.ShopRemote

local connection 
connection = Remote.OnServerEvent:Connect(function(player, ...)
	--check if the event is valid, and it wont fail
	connection:Disconnect()
	--run the code
end)

PS: You can also destroy the remote, but it may break your client scripts if they rely on it.

It doesn’t fire as soon as the script runs; it waits for a user to click a button, then fires. The script is placed inside of a ScreenGui. And the remote event is designed to be used throughout the server’s lifespan. not just a few times

I’m not sure if I explained this correctly, but it fires the number of players times to the server and back.

If it fires #players times for each player you may have a player loop around the remote or the server(although that’s just a guess, without the code snippets we can’t do much).

theres no loops based on the # of players whatsoeover. i checked all the scripts it doesnt mention the number of players anywhere within the remote events.

1 Like