luaroblox

How to activate respawn after character death?


how should I make this code work, after the character is dead, activate the respawn function when I press the button. At this moment the button works, but it does not take into account whether the character is dead or not, and I want to put the condition to operate the button only when the character is dead. eventually, I can post an error message or something, let the man know why the button doesn't work.

  model = game.Workspace.MyModelName -- Replace with your model name
messageText = "Regenerating MyModelName..."

message = Instance.new("Message")
message.Text = messageText
backup = model:clone()
enabled = true

function regenerate()
    message.Parent = game.Workspace
    model:remove()

    wait(4) -- display regen message for 4 seconds

    model = backup:clone()
    model.Parent = game.Workspace
    model:makeJoints()
    message.Parent = nil

    enabled = false
    wait(30)
    enabled = true
end

function onHit(hit)
    if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled then
        regenerate()
    end
end

script.Parent.Touched:connect(onHit)
    script.Parent.ClickDetector.MouseClick:connect(onTouched)

Solution

  • Add the following to your code.

    game.Players.PlayerAdded:Connect(function(plr)    
    
        plr.CharacterAdded:Connect(function(char)
              Char.Humanoid.Died:Connect(regenerate)
        end)
    
    end)