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.
Redundant Connections: You’re connecting PlayerAdded and CharacterAdded events twice, which can lead to race conditions and conflicts.
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:
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.
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.
Function onPlayerAdded: This simply connects the CharacterAdded event for the player to the onCharacterAdded function.
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.
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