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

welding part to head without touched event?

Asked by
Probix 80
8 years ago

I want the Part to weld to the Character's Head when the player spawns into the game. But, welding don't have the PlayerAdded event. How would I go about this?

function JustWelding (hit)

    local humanoid = hit.Parent:FindFirstChild("Humanoid")

        if humanoid then
            local head = hit.Parent.Head
            local torso = hit.Parent.Torso
            script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
            local weld = Instance.new("Weld")
            weld.Part0 = torso
            weld.C0 = torso.CFrame:inverse()
            weld.Part1 = script.Parent
            weld.C1 = script.Parent.CFrame:inverse()
            weld.Parent = script.Parent
            script.Parent.Anchored = false
        end
end

script.Parent.Touched:connect(JustWelding)

It works great with the touched event! Now I need it to do it when the character spawns into the game.

Help would be appreciated!

1 answer

Log in to vote
3
Answered by 8 years ago
Edited 8 years ago

For this, it would be better to use the CharacterAdded event rather than PlayerAdded, as the former will also run after the player respawns. You will need the PlayerAdded to connect a function to CharacterAdded, however. You will need to use the Character property of Player to get the player's character and use that for the function instead.

function JustWelding (character)
    local head = character.Head
    local torso = character.Torso
    script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
    local weld = Instance.new("Weld")
    weld.Part0 = torso
    weld.C0 = torso.CFrame:inverse()
    weld.Part1 = script.Parent
    weld.C1 = script.Parent.CFrame:inverse()
    wait() -- This seems to fix the problem.
    weld.Parent = script.Parent
    script.Parent.Anchored = false
end

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(JustWelding)
end)

0
Nothing happen. Probix 80 — 8y
0
Fixed the problem. Not sure why this works; I haven't used welds in a while. IDidMakeThat 1135 — 8y
0
Thank you. Probix 80 — 8y
Ad

Answer this question