Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I can't figure out how to change object values when a player leaves, any help?

Asked by 6 years ago

Hey, anyone who is reading this

I've been making a script but only the playeradded seems to work and not remove, if anyone could help that would be nice.

--Get the workspaces player folder so we can later on set values to clone player--
local player_folders = game.Workspace:WaitForChild("Players")
--  --


--Get each individual player object value--
local player1 = player_folders:WaitForChild("Player1")
local player2 = player_folders:WaitForChild("Player2")
local player3 = player_folders:WaitForChild("Player3")
-- --

--Set the value to an entered player, if a value is free--
game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(char)
        wait(0.5)
        if player1.Value == nil then
            player1.Value = char
        elseif player2.Value == nil then
            player2.Value = char
        elseif player3.Value == nil then
            player3.Value = char
        end
    end)
end)
-- --

game.Players.PlayerRemoving:connect(function(plr)
        wait(0.5)
        if player1.Value == plr.Character then
            player1.Value = nil
        elseif player2.Value == plr.Character then
            player2.Value = nil
        elseif player3.Value == plr.Character then
            player3.Value = nil
        end
end)

Thanks to anyone who can help!

0
Does the wait(.5) in the PlayerRemoving event affect anything if removed? My guess is that the event is referencing a player that has already disconnected. PreciseLogic 271 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
    if player1.Value == plr.Character then
            player1.Value = nil
        elseif player2.Value == plr.Character then
            player2.Value = nil
        elseif player3.Value == plr.Character then
            player3.Value = nil
        end

It's not ever going to find the right conditions. The player's character no longer exists, once they leave. Therefore, it'd probably be a better idea to do something like if player1.Value.Name == plr.Name then. I'm sorry if I missed anything, but the main idea is you can't search for the character.

0
Thanks for that, didn't realise that you couldn't search for character! arceus3270 2 — 6y
Ad

Answer this question