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

How to reset after character dies?

Hello,

Yesterday I tried to reset with those codes below but it didn't work.

I have many animations, I give an example: it changes properties when I click on the button.

button3 = script.Parent

function ClickButton ()
	if button3.BrickColor == BrickColor.new('Persimmon')  then
		button3.BrickColor = BrickColor.new('Mulberry')
	else
		button3.BrickColor = BrickColor.new('Persimmon')
	end
end

button3.ClickDetector.MouseClick:connect (ClickButton)

I made a clone into Lighting. I created a Script with this code:

local clone = workspace.Animations.button3:Clone()
clone.Parent = game.Lighting

(button3 is inside Workspace > Animations (a folder))

After, i create a Script inside ServerCharacterScripts, with this code:

script.Parent.Humanoid.Died:Connect(function() 
for i,v in pairs(workspace:GetDescendants()) do 
  if v:IsA("Script") then
     v:Destroy()
   end
end
for i,v in pairs(game.Lighting:GetDescendants()) do 
  if v:IsA("Script") then
        v:Clone().Parent = workspace 
      end
   end
end)

When I play, I have two problems:

  • the button keeps changed properties after character dies
  • I can't click anymore to change properties after death

I'd like that when the character dies, the player restart the game at the last spawn and repeat all the ''operations'' from the last CheckPoint.

Thank you for your help.

2 answers

✓ Accepted answer

I think this is a better alternative than listening to a humanoid dying since this fires when a character dies and his player is/has been removed.

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterRemoving:Connect(function()
		-- do code
	end)
end)

5 comments

0

Thank you for your answer. Where can I put this code? I replace ServerCharactersScripts code by this one? and --do code, what are you talking about?

0

Do code means the rest of your script, like changing the colours. This is a server script so put it anywhere but not in the character scripts or player scripts.

0

It works if it's clicked. I read the explaination below.

0

Pardon?

0

Sorry, I write* just below (as second answer) to explain the problem I still have.

It works if the button were clicked before death. But when it's not clicked, it changes as if it was clicked, after respawn. I put this code in a script into ServerScriptService.

button3 = script.Parent.Parent.Workspace.Animations.button3

game:GetService("Players").PlayerAdded:Connect(function(Player)
	    Player.CharacterRemoving:Connect(function(ClickButton)
        if button3.BrickColor == BrickColor.new('Persimmon')  then
        button3.BrickColor = BrickColor.new('Mulberry')
    else
        button3.BrickColor = BrickColor.new('Persimmon')
    end
    end)
end)

button1 = script.Parent.Parent.Workspace.Animations.button1

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.CharacterRemoving:Connect(function(ClickButton)
        if button1.Transparency == 0 then
		button1.Transparency = 0.5
	else
		button1.Transparency = 0
	end
    end)
end)

2 comments

0

That's because you're changing the colour to its clicked state. Just set the colour to Mulberry without using an if condition. And you're doing the same for button 1 just set its transparency to 0.5 directly.

0

Original was Persimmon and Transparency = 0. Now, it works with it. Thank you very much for your help :D

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