WAIT() for player character

how to wait for a players character in the code?

5 Likes

You can do this in a local script

local Player = game.Players.LocalPlayer
Player.CharacterAdded:Wait()

Also this one

local Player = game.Players.LocalPlayer
Player.CharacterAdded:Connect(function()

end)
3 Likes

works fr fr? fr? fr? fr? fr? fr?

3 Likes
			local Char_One = PLR_ONE.CharacterAdded:Wait()
			local Char_Two = PLR_TWO.CharacterAdded:Wait()
			--PLAYER LEFT, HOW TO SOLVE???
1 Like

At that point Char_Num would be nil

1 Like
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

player:GetPropertyChangedSignal("Parent"):Connect(function()
   --player left
end)
1 Like

Never tried this, but wouldn’t this function just not run since the player leaves before it actually happens?

1 Like

being away from my country i live in for 3 weeks has kinda screwed me up, huh

2 Likes

Yeah no problem. Also I’m not sure what the poster means by “player left, how to solve” so I’m just throwing out info and guessing what they’re asking.

1 Like
local characters = {}

game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
      characters[player.Name] = character
   end)
end)

game.Players.PlayerRemoving:Connect(function(player)
   characters[player.Name] = nil
end)
2 Likes

Wouldn’t the character automatically be set to nil when they leave since the model would be destroyed?

1 Like

just a backup incase it doesnt get set automatically.

1 Like

The best way to get the character in my opinion is to do:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

Hope this helps!

i just dont actually know how to make my script not crash and stop wortking forever.

What do you mean by “crash and stop working forever”

image
what if PLR_ONE leaves the game?
while the process?

if PLR_ONE and PLR_TWO then
    -- They're still in the game
end

Edit: Reading this again I realize this only works if they leave in the infinitesimally small time between the lines.
I changed the code
Also if you’re doing :Wait() I’m pretty sure it will infinitely delay the rest of the code. I don’t think you have to do this, maybe check if their character exists and if not find a new player to use.

1 Like