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?
01 | function 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.Part 0 = torso |
11 | weld.C 0 = torso.CFrame:inverse() |
12 | weld.Part 1 = script.Parent |
13 | weld.C 1 = script.Parent.CFrame:inverse() |
14 | weld.Parent = script.Parent |
15 | script.Parent.Anchored = false |
16 | end |
17 | end |
18 |
19 | 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!
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.
01 | function 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.Part 0 = torso |
07 | weld.C 0 = torso.CFrame:inverse() |
08 | weld.Part 1 = script.Parent |
09 | weld.C 1 = script.Parent.CFrame:inverse() |
10 | wait() -- This seems to fix the problem. |
11 | weld.Parent = script.Parent |
12 | script.Parent.Anchored = false |
13 | end |
14 |
15 | game.Players.PlayerAdded:connect( function (player) |
16 | player.CharacterAdded:connect(JustWelding) |
17 | end ) |