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

How do I make this repeat on respawn?

I want to attach a part to a character when they spawn in which works when joining the game but not when respawning and I haven't been able to figure it out

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local Hitbox = game.ReplicatedStorage.Hitbox:Clone()
	Hitbox.Parent = character	 

	local Weld = Instance.new("Weld", Hitbox)
	Weld.Name = "Weld"
	Weld.Part0 = Hitbox
	Weld.Part1 = character.HumanoidRootPart
	end)

1 answer

✓ Accepted answer

You'll need to create an event that detects when the player's character spawn in, like this:

--function to make whole thing easier without having to rewrite it over again
function createHitbox(character)

local Hitbox = game.ReplicatedStorage.Hitbox:Clone()
    Hitbox.Parent = character    

    local Weld = Instance.new("Weld")
    Weld.Name = "Weld"
    Weld.Parent = Hitbox
    Weld.Part0 = Hitbox
--Added wait for child to make sure humanoid root part is part of the character first
    Weld.Part1 = character:WaitForChild("HumanoidRootPart")
	

end

game.Players.PlayerAdded:Connect(function(player)
--[[ 
this is to make sure it creates it the first time the player joins in case their character gets added --to the game before the event is established:
--]]
    local character = player.Character or player.CharacterAdded:Wait()
    createHitbox(character)    



--[[
this is to make it keep happening each time they respawn:
--]]
player.CharacterAdded:Connect(function(char)
createHitbox(char)
end)

    end)

Hope I helped!

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