I’m trying to get a hat to parent to the new player’s character after the player dies. Anyone know how to?
(Example)
-- Yes, I know this is client-sided. It's meant to be like that.
local Player = game:GetService("Players").LocalPlayer;
local Humanoid = Player.Character:FindFirstChild("Humanoid");
local Hat = game:GetService("Workspace"):FindFirstChild("Hat");
Humanoid.Died:Connect(function()
Hat.Parent = newCharacter -- newCharacter doesn't exist, anyone know how to get it?
end)
Why would you parent a hat to a dead character, it would disappear once they respawn.
And, can’t you just parent it without waiting for them to respawn?
My intention with the script isn’t to parent a hat to a dead player’s character. It’s just an example which makes understanding easier. In reality, I’m making client-sided visualizers and when the character dies, I want it to parent it to a folder until the new character’s respawned and then parent it to the new character again.
local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if game.YourStorage:FindFirstChild("Hat") then --Storage area
game.YourStorage.Hat.Parent = Character
end
end)
end)
Character.Humanoid.Died:Connect(function()
local Hat = Character.Hat --Get the hat
Hat.Parent = game.YourStorage --Storage area idk maybe a folder
end)
In that case, here’s some pseudocode for you, that you can work with.
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local function HumanoidDied()
print(('Player %s died!'):format(Player.Name))
-- parent their hat.
end
Humanoid.Died:Connect(HumanoidDied)