Need Help with Humanoid Script Error

Here is the script:

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            print(player.Name .. " has died.")
        end)
    end)
end

Players.PlayerAdded:Connect(onPlayerAdded)

local function respawnHumanoid(humanoid)
    humanoid.Health = 100
    humanoid:MoveTo(Vector3.new(0, 10, 0))
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
            respawnHumanoid(humanoid)
        end
    end)
end)

The script is supposed to print a message when a player dies and then respawn the player’s humanoid at a specific location. However, it doesn’t seem to be functioning as expected. Sometimes the message doesn’t print, or the respawning doesn’t work correctly.

1 Like

Issues:

  1. Redundant Connections: You’re connecting PlayerAdded and CharacterAdded events twice, which can lead to race conditions and conflicts.
  2. Timing: The order of operations might cause issues, especially with WaitForChild and FindFirstChild.

Solution:

We should ensure a clear sequence of event handling and avoid redundant connections. Here’s a revised version of the script:

local Players = game:GetService("Players")

local function respawnHumanoid(humanoid)
    humanoid.Health = 100
    humanoid.Parent:MoveTo(Vector3.new(0, 10, 0))
end

local function onCharacterAdded(character)
    local humanoid = character:WaitForChild("Humanoid")
    
    -- Handle humanoid death
    humanoid.Died:Connect(function()
        print(character.Name .. " has died.")
        respawnHumanoid(humanoid)
    end)

    -- Initial respawn setup
    respawnHumanoid(humanoid)
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Explanation:

  1. Function respawnHumanoid: This function sets the humanoid’s health and moves the character to the desired respawn location. Note that MoveTo is called on the humanoid’s parent, which is typically the character model.
  2. Function onCharacterAdded: This handles everything that needs to happen when a character is added. It waits for the Humanoid child, connects the Died event to print the death message, and calls respawnHumanoid for initial respawn positioning.
  3. Function onPlayerAdded: This simply connects the CharacterAdded event for the player to the onCharacterAdded function.
  4. Connecting Events: Finally, we connect the PlayerAdded event to the onPlayerAdded function.

Key Changes:

  • Simplification: By defining onCharacterAdded and connecting it only once, we avoid redundant connections and potential timing issues.
  • Initial Respawn: The initial call to respawnHumanoid ensures the character is moved to the respawn location right after it is added.

This approach should provide more consistent behavior and avoid the issues you were experiencing.

Thank you I will try this out to see if it works I am testing it soon though but in the mean time didn’t it get any console error ?

1 Like

Nope, I tested the script out and everything works. No errors.

thanks but i found the solution here

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            print(player.Name .. " has died.")
        end)
    end)
end

Players.PlayerAdded:Connect(onPlayerAdded)

local function respawnHumanoid(humanoid)
    humanoid.Health = 100
    humanoid:MoveTo(Vector3.new(0, 10, 0))
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChild("Humanoid")
        if humanoid then
            respawnHumanoid(humanoid)
        end
    end)
end)

X-axis is a nice move to method

i just realized it actually prints but ALSO moves to the point but thank you! i will try y-axis

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.