Loop stopping for everyone when it should be stopping for only 1 player

Hello! I am having an issue with loops. To explain my problem, I first need to explain what this is supposed to do.

Basically, this loop is inside of a function that is called when a player fires a remote event. It will start counting until the player submits their score. There is a lot more in this script, but I took it out because it isn’t the issue.

The Problem:
When multiple people have started a loop for themselves, if one person submits, it submits for every player in the lobby that had the loop running.

I only want the loop to stop for that one player who submitted, and I have tried looking for a solution by searching up some sort of “local loop” that I can run on the server, but it doesn’t look like that exists. I also checked to see if there was another type of loop I could use to fix this issue.

	                local r = 0
					local timer = 0
					repeat
						timer = timer + 1
						
						print(player.Name..": "..timer)
						SubmitEvent.OnServerEvent:Connect(function(player, sentDistance)
							r = 1
                            -- Alot more was here, but I stripped it down to what the problem really is.
						end)
						wait(1)
					until r == 1

If you would like more information, please let me know. Thanks!

1 Like

Assuming this is a server script, from what I can see I think I know what the issue is here.
I recommend using Remote Events to achieve this. You can read about them in the hyperlink.

What you could do is:

  • Have a local script inside every player
  • Have a Remote Event which fires from the player to the server containing the player’s score.

This loop should be done locally through a local script, as it will be stopping at different times for different people. To tell the players to start the loop, fire a remote event to all clients from the server. Once the clients receive this message, include this loop inside a .OnClientEvent function. When the player submits their score, send the server the score so that it knows what the player has submitted.

Tell me if you would like me to explain in further detail.

Well actually, this loop is already inside of a remote event function. Here is the updated script with the function.

Server Script:

             StartEvent.OnServerEvent:Connect(function(player, force) -- fires when player clicks the start button in their player gui
                    local r = 0
					local timer = 0
					repeat
						timer = timer + 1
						
						print(player.Name..": "..timer)
						SubmitEvent.OnServerEvent:Connect(function(player, sentDistance) -- fires when the player clicks the submit button in their gui
							r = 1
                            -- Alot more was here, but I stripped it down to what the problem really is.
						end)
						wait(1)
					until r == 1
             end)

I forgot to mention that it will also be starting at different times for people, so I cant make an .OnClientEvent. And yes, this is a server script. It is apart of an anti cheat I created to prevent false values. That is also why I cannot put this into a local script, as submitting any value without a server sided check will make this very insecure.

Sorry if my explanation was not very good, hopefully this is a bit better.

I’m not very sure if this is possible in a server script. There might be a way I’m not aware of but as far as I know this might not be achievable with your approach. If you want a server sided check, you can check the value after it has been submitted (client → server, server checks value, stops loop).

Yeah, that’s exactly what the actual system does but I stripped it down to the very basics to make it less difficult to read. When you stop the loop, it is just stopping the loop for all of the other people that are currently running it for some reason. Thanks for the help.

1 Like

Everything on the server automatically gets replicated to all clients, so the only way to stop it for one person is by firing an event for a specific player, which then requires the loop to be in the client.

Yes, I already knew this, but I thought that since it is inside of the OnServerEvent it would only alter the player that fired the remote function. Hopefully I can find some solution.

Ah, I see. You can imagine that function as a gate, when one person fires the event it opens the gate. Other people can also fire the function though, but since there is only one gate, they just walk in to the one which is already open, thus all the changes happening to that first person will happen to the second person.

But do you think it is possible to only end the loop for that one person? I cant think of any possible solution. There must be someone who has done this before.

The only solution I can think of is to make everything client sided. (Every player has their own gate, so they don’t interfere)

Unfortunately that wouldn’t work for my situation. Hopefully someone out there can think of a server-sided solution. Thank you for dedicating some of your time to trying to help me out, it is greatly appreciated. :slightly_smiling_face:

No problem, feel free to post again if you have any further queries.