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?

01function JustWelding (hit)
02 
03    local humanoid = hit.Parent:FindFirstChild("Humanoid")
04 
05        if humanoid then
06            local head = hit.Parent.Head
07            local torso = hit.Parent.Torso
08            script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
09            local weld = Instance.new("Weld")
10            weld.Part0 = torso
11            weld.C0 = torso.CFrame:inverse()
12            weld.Part1 = script.Parent
13            weld.C1 = script.Parent.CFrame:inverse()
14            weld.Parent = script.Parent
15            script.Parent.Anchored = false
16        end
17end
18 
19script.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.

01function JustWelding (character)
02    local head = character.Head
03    local torso = character.Torso
04    script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
05    local weld = Instance.new("Weld")
06    weld.Part0 = torso
07    weld.C0 = torso.CFrame:inverse()
08    weld.Part1 = script.Parent
09    weld.C1 = script.Parent.CFrame:inverse()
10    wait() -- This seems to fix the problem.
11    weld.Parent = script.Parent
12    script.Parent.Anchored = false
13end
14 
15game.Players.PlayerAdded:connect(function(player)
16    player.CharacterAdded:connect(JustWelding)
17end)
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