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

How would I make the script restart if the player died?

Asked by 9 years ago

Hi Ive been trying to figure out something but it wont just pop in my head, Im trying to make a script so if the player dies then the script will restart all over. heres the code, any help?

randplayer = math.random(1,#players) playerchosen = players[randplayer] h.Text = "The name of the player is" ..playerchosen.Name wait(1) playerchosen.Character.Torso.CFrame = CFrame.new(Vector3.new(43.088, 26.76, -333.25))

2 answers

Log in to vote
2
Answered by 9 years ago

You should probably just make that all a function that occurs on death. Like this:

playerchosen.Character.Humanoid.Died:connect(function() --Stuff you said end)

Ad
Log in to vote
2
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

The code cheez55 mentioned will cause the game to break if the player disconnects instead of dying. I recommend you try something like this instead:

randplayer = math.random(1,#players)
playerchosen = players[randplayer]
h.Text = "The name of the player is" ..playerchosen.Name
wait(1)
playerchosen.Character.Torso.CFrame = CFrame.new(Vector3.new(43.088, 26.76, -333.25))
-- Here's the part I added:
function RestartScript()
    script:Clone().Parent = Game.ServerScriptService -- clone the script so it restarts
    script:Destroy() -- disconnect all events in this one so it doesn't do unexpected things
end
playerchosen.Character.Humanoid.Died:connect(RestartScript) -- if the player dies
playerchosen.AncestryChanged:connect(RestartScript) -- if the player disappears from the game

Answer this question