Wins gained automatically instead of being earned

I am trying to make a sword fight game on Roblox. Everything goes well, until the game starts. Then it gives a player a win in the leaderstats instantly instead of earning it in the game. The following code is below:

for i = gameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				local name = player.Name
				local character = game.Workspace:WaitForChild(name)
				
				if not character then
					-- Left the game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.." is still in the game")
					else
						-- They are dead
						table.remove(plrs,x)
						print(player.Name.." has been removed")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.." has been removed")
			end
		end
		
		Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left! Try to stay alive!"
		
		if #plrs == 1 then
			-- Last person standing
			Status.Value = plrs[1].Name.." has won!"
			plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + reward
			break
		elseif #plrs == 0 then
			Status.Value = "Nobody won..."
			break
		elseif i == 0 then
			Status.Value = "Time's up! Nobody won!"
			break
		end
		
		wait(1)
	end
	
	print("End of game")

Could you please help?

Do you remove the GameTag when they leave?

for i, player in pairs(game.Players:GetPlayers()) do
		local name = player.Name
		local character = game.Workspace:WaitForChild(name)
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

Is this what you are looking for?

Thanks.
If a player enters the game and no other player does do you give that one player a win?

if one player is still alive then this statement is going to be true for every player it loops over

Are there some timing process involved here?
For example player 1 joins the game so the game playing clock should be held off until
player 2 joins the game so the game playing could start but is it a game with only 2 players? Do you have a minimum number of players involved somewhere?
Is there something to track how long a player has been playing?
Do you have a minimum play time setting somewhere to ensure that players have had time to enjoy the experience?

no it happens so fast its not going to matter if the player leaves mid way it would matter if the wait was like mid way through the code but its after everything runs in a flash

It’s not only a 2 player game. It’s supposed to be a “last person standing” type of game. I have a video, but I am having trouble uploading it. Also, the game says “Waiting for players” until 2 players join, but I might change the number of players required to play in the future.

No, we wait until there are 2 or more players. Then the game will start.

Okay. What should I do to fix this?

Nevermind, I have fixed this problem! Thanks for your advice and help!
If you want the source of where I found this, click here to see the full discussion!

1 Like