Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make this repeat on respawn?

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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!

Ad

Answer this question