So I used a script that made a character respawn, but directly in the line after it I used another script that is suppose to locate the player’s character. No matter what I do, it always detects the character before the player respawned. How can I wait until the player is done respawning and then detect the character?
Use .CharacterAdded, but it mainly depends on how you structured your scripts (linearly, parallel).
local character = nil
player.CharacterAdded:Connect(function(newCharacter)
print(newCharacter.Name)
character = newCharacter
end
Can you tell me what is “player” equal to?
The player in a LocalScript
local Players = game:GetService("Players")
local player = Players.LocalPlayer
Well the script is a server script, and it’s not trying to get the local player. It’s looping through all the players. Also the script didn’t work because it’s an event, but I’m trying to wait until the player is done respawning.
Then loop through every player instead and tie a function for when they respawn. I don’t see why it being an event is a bad thing since it fires right after the Character model is added to workspace.
local Players = game:GetService("Players")
local function onCharacterAdded(character)
-- Code
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
I am looping through every player. Also I tried using the function and it broke my game.
Do you have the code for what you want to do? The code I provided activates as soon as a player spawns. If your code is in another function and it’s waiting for a character, then you could do:
local character = player.Character or player.CharacterAdded:Wait()
Well it did work but it still detected the character before the player respawns
Add wait(game:GetService("Players").RespawnTime)
maybe?