How to prevent my game from attempting to award currency to a player who has left?

I’m making a game that involves currency distribution to players, as most games of this type should. However, one problem I am consistently running into is the fact that, irregardless of me removing a player from a variable-defined table, the game still tries to award that player with currency after they left (resulting in an error). Here is the code from the two parts of the script that manage player-table removal and currency awarding:

PlayerRemoving Function

game.Players.PlayerRemoving:Connect(function(plrleft)
		if chosenbeast.Name == plrleft.Name then
			stop = true
			local leftbeast = plrleft.Name[livingplayers]
			--fadeevent:FireAllClients("", "rbxassetid://0")
			wincondition = "Players"
			table.remove(livingplayers, leftbeast)
			table.remove(realplayers, leftbeast)
		else
			local left = plrleft.Name[livingplayers]
			table.remove(livingplayers, left)
			table.remove(realplayers, left)
			--play death sound and remove player from living table
		end
	end)

Currency Awarding

if wincondition == "Players" then
		print(#realplayers)
		for i,v in pairs(realplayers) do
			if game.Players:FindFirstChild(v.Name[realplayers]) == true then
				if v.Name == chosenbeast.Name then
					game.Players[v.Name].leaderstats.Coins.Value = game.Players[v.Name].leaderstats.Coins.Value + 20
				elseif v.Name ~= chosenbeast.Name then
					game.Players[v.Name].leaderstats.Coins.Value = game.Players[v.Name].leaderstats.Coins.Value + 50
				end
			end
		end
	elseif wincondition == "Beast" then
		print(#realplayers)
		for i,v in pairs(realplayers) do
			if game.Players:FindFirstChild(v.Name[realplayers]) == true then
				if v.Name == chosenbeast.Name then
					game.Players[v.Name].leaderstats.Coins.Value = game.Players[v.Name].leaderstats.Coins.Value + 50
				elseif v.Name ~= chosenbeast.Name then
					game.Players[v.Name].leaderstats.Coins.Value = game.Players[v.Name].leaderstats.Coins.Value + 20
				end
			end
		end
	end

This error made me quit development for a while, so I’d really like to solve it so I can move on to other things! Thanks in advance for any assistance.

I think it’s the way you are accessing the table, you are doing player.Name[realplayers] which essentially tries to find the table in the players name, while you need to do realplayers[player.Name] to try to find the players name inside the table

1 Like

Why do you need to use table realplayers, you could directly use game.Players:GetChildren() and that will return players currently in game.

1 Like

Because I am creating a table of eligible players for that round. If I wanted to get every player regardless of condition, of course I’d use :GetPlayers()

This doesn’t seem to work, it worked the original way without someone leaving.

Well then I guess you have to change it to what @SMG_Guy already proposed.

Instead of trying to add/remove into player.Name, which by the way I dont think is even possible so I don’t know how your script works. You will have to create 2 separate tables

local realPlayers = {}
local livingPlayers = {}

and then just call

table.add(realPlayers, player) -- to add player
table.remove(realPlayers, table.find(realPlayers, player)) -- to remove, you might need to check if the player is in the table first so it doesn't throw an error.

I had no idea table.find was an actual thing

I found another way to fix the problem though. Thanks for the tip though!

1 Like

Yeah! table.find(table, value) returns index of the table that the value is indexed on, if it doesn’t find it it returns nil!

1 Like

It’s table.insert not table.add