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.