Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this Quick Character Respawn script work?

So I have a kill brick that kills the player when they touch it, then quickly respawns them one second later. However, the problem is, after the Player is respawned, the Player Respawns again a couple seconds later. I think this is due to the actual death automatically making the player respawn seconds later, overlapping my script. Any idea how to fix this?

debounce=false
function onTouch(Brick)
	if Brick.Parent:FindFirstChild("Humanoid")and debounce==false then
		if Brick.Parent.Humanoid.Health>0 then
			debounce=true
			Brick.Parent.Humanoid.Health=0
			local Player=game.Players:FindFirstChild(Brick.Parent.Name)
			wait(1)
			Player:LoadCharacter()
			local Human=game.Workspace:WaitForChild(Player.Name)
			Player.Character:MoveTo(game.Workspace.SFLavaT2.Position)
			debounce=false
		end
	end
end
script.Parent.Touched:connect(onTouch)

3 answers

✓ Accepted answer

Disable "CharacterAutoLoads" in "Players." This property handles both respawns and initial spawning so you'll have to load the player's character after connecting.

You can do this with a player added event like this:

game.Players.PlayerAdded:connect(function(CHILD)
	CHILD:LoadCharacter()
end)

NOTE: In studio, if you press the play solo button the player will tend to load before this script is initialized, so you would have to type this into the command line if it fails.

game.Players.Player:LoadCharacter() 

If you're not logged in "Player" will be replaced with "Player1."

EDIT: Did more helping c:

How about you try putting a wait time at the end of the Function and try setting the debounce value to true right after the If Statement and put it to false at the very end just before the Function ends. So try something like this.

--[[
It's good to keep your code spacious and clear and indented however, I'm going to fix a couple of things in there so I will just do it like this but, I advice you to keep your code clean, indented, with some comments telling as to what is going on in the next few lines of code it helps you and the other coders you might be coding with even if you're not coding with others it could help you locate certain parts of your script if your looking for something.
--]]
debounce=false
function onTouch(Brick)
    if Brick.Parent:FindFirstChild("Humanoid")and debounce==false then
        debounce=true
        if Brick.Parent.Humanoid.Health>0 then
            Brick.Parent.Humanoid.Health=0
            local Player=game.Players:FindFirstChild(Brick.Parent.Name)
            wait(1)
            Player:LoadCharacter()
            local Human=game.Workspace:WaitForChild(Player.Name)
            Player.Character:MoveTo(game.Workspace.SFLavaT2.Position)
        end
    end
wait(3)
debounce=false
end

script.Parent.Touched:connect(onTouch)

I hope this helped even if it was a little thank you for reading all the way.

~~KingLoneCat

Would this be better?:


game.Players.Player.Character.Humanoid.Died:connect(
function()
plrX=game.Players.Player.Character.Torso.Position.X
plrY=game.Players.Player.Character.Torso.Position.Y
plrZ=game.Players.Player.Character.Torso.Position.Z
game.Players.Player:LoadCharacter()
game.Players.Player.Character:MoveTo(plrX, plrY, plrZ)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.