How do I improve this little script?

Hey,

I want that after you joined the game the player gets resetted.
But if I use this code, the player keeps getting resetted because of the CharacterAdded function :confused:

game.Players.PlayerAdded:Connect(function(player)
	print('Player Added !')
game.Players.RespawnTime = .2
	player.CharacterAdded:Connect(function(char)
		wait(5)
	char.Head:Remove()
	end)
	end)
2 Likes

It’s because your constantly removing the characters Head upon creation. I suggest doing something like this.

game.Players.PlayerAdded:Connect(function(Player)
	game.Players.RespawnTime = 0.2
	local Character = Player.CharacterAdded:Wait()
	Character:WaitForChild("Head"):Destroy()
end)

you could also just Destroy the character

1 Like

Oh, thank you. I will try it out :smiley:

1 Like

btw instantly removing the characters head upon join will not reset the player.

I just added a wait(5) but somehow the character doesn’t reset for me.

Try just setting the humanoids health to 0

Why are you removing the head? Can’t you just do humanoid:TakeDamage(humanoid.MaxHealth)?

For some reason you can’t kill a character upon join :confused:

If you want to respawn the player after they join, you could use Player | Roblox Creator Documentation

An example would be:

game.Players.PlayerAdded:Connect(function(Player)
    Player:LoadCharacter()
end)

Instead of adding a wait(5), you could try adding CharacterAdded:Wait() so the script only runs until the character is fully incorporated into the game. I think @Arthur_Grandwell already used this in his script but I’d like to remind you still of this function.

if not player.Character then player.CharacterAdded:Wait() end

game.Players.PlayerAdded:Connect(function(Player)
	game.Players.RespawnTime = 0.2
	local Character = Player.CharacterAdded:Wait()
	wait(10)
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid:TakeDamage(Humanoid.MaxHealth)
	
end)

Like that?

The wait(10) is unnecessary, as you are already using CharacterAdded.

you should mark @Poneware as the solution and use his code

Ik, but I want that it only happens 5 or 10 secs after the char spawned

Just do

game.Players.PlayerAdded:Connect(function(Player)
	game.Players.RespawnTime = 0.2
Player.CharacterAdded:wait()
	wait(5)
	Player:LoadCharacter()
end)

You might as well change to respawn time manually in the explorer and you can switch the wait with task.wait if you want to wait a few seconds after the char spawns

I see.

If you want to do this, you could try using task.wait() instead as wait() pauses your thread and task.wait() continues normally. I believe wait() also has some small performance issues.

(Here’s a link with more informations about task.wait())

task.wait() still yields the thread, it does the same as wait() but with the new task scheduler.
You could use task.delay() instead as that takes a function and doesn’t yield the current thread.

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	game.Players.RespawnTime = 0.2
	local Character = Player.CharacterAdded:Wait()
	task.wait(20)
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid:TakeDamage(Humanoid.MaxHealth)
	
end)

Like that?

game.Players.PlayerAdded:Connect(function(Player)
	game.Players.RespawnTime = 0.2
	Player.CharacterAdded:Wait()
	task.delay(20, function()
		Player:LoadCharacter()
	end)
end)

Edit: Yes load character does reset the character

Edit 2: did you wait 20 seconds, because it works fine for me