Hi, i’m trying to add 1 to the ‘win’ stat to a specific player when another player leaves. Basically there are two players in a 1v1 and if one of them leaves the game then the other player should have + 1 to their ‘win’ stat. I don’t really understand as my friend was helping me with it a while ago, but for some reason its not working and has got red lines under some parts of it. Any idea whats wrong?
local Plr = {
}
table.insert(Plr, player1)
table.insert(Plr, player2)
local player1 = "Player1"
local player2 = "Player2"
game.Players.PlayerRemoving:Connect(function(p)
if p.Name == player1 then
table.remove(player1, p)
for _.Plr in pairs(player2) do
Plr:WaitForChild("leaderstats").Wins.Value += 1
end
elseif p.Name == player2 then
print(p.Name, "Has Left")
end
end)
local Plr = {}
local player1 = "Player1"
local player2 = "Player2"
table.insert(Plr, player1) -- you're trying to add these to a table before you've defined them, make sure they're below where you define them.
table.insert(Plr, player2)
game.Players.PlayerRemoving:Connect(function(p)
if table.find(Plr, p.Name) then
table.remove(Plr, table.find(Plr, p.Name)) -- might be better to showcase the usage of table.find and table.remove here.
for i, v in pairs(Plr) do -- might be a better idea to use Plr[1] here if theres only going to be 1 value left. As previously stated you've declared your loop incorrectly.
if game.Players:FindFirstChild(v) then
local Player = game.Players[v]
Player:WaitForChild("leaderstats"):WaitForChild("Wins").Value += 1
end
end
end
end)
These variables are previously assigned to the actual player, so i think ill change that. But to be honest I don’t really know what my friend was doing. If you have any suggestions I would love to hear them, I’ll take no offence. Thanks
Thank you very much, I just tried out the script and it didn’t add a win to the player that didn’t leave the game for some reason. I didn’t have any error messages though. Any idea why?
My mistake. You’re storing a string yet table.find is searching for the instance of the player rather than the name. Change table.find(Plr, p) to table.find(Plr, p.Name) and it should remedy the issue. I’ve updated my code to reflect this issue!