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

Weld on player join?

Asked by
k1nq 30
8 years ago

So, I was messing around and trying to weld a part to my characters head when I joined, but it didn't seem to work and there were no errors.

game.Players.ChildAdded:connect(function(hit)
    local humanoid = hit:findFirstChild("Humanoid")
    if humanoid then
        local head = hit.Parent.Character.Head
        local torso = hit.Parent.Character.Torso
        script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0)
        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)

2 answers

Log in to vote
1
Answered by
jakedies 315 Trusted Moderation Voter Administrator Community Moderator
8 years ago

Your player and character are two difference things. When you do game.Players.ChildAdded you wait until an object is added in the Players service. When someone joins, their player is added to that service BUT their player is not their character. Things like Head/Torso/Humanoid/etc. exist in their Character so what you would want to do is run that when their Character is added.

local function characterAdded(character)
  -- do stuff with character (where Head/Torso/Humanoid should be, however you should make sure they exist first)
end

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(characterAdded)
end)
1
A good way to make sure they exist: use the WaitForChild method. For example `local humanoid = character:WaitForChild("Humanoid")` jakedies 315 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

here you go :D just place this script inside your spawn location /spawn point ->and then place your part inside a model called "Helmet" and place this Helmet model into your spawn point -get sure that your part is anchored

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Helmet") == nil then
        local g = script.Parent.Parent.Helmet:clone()
        g.Parent = hit.Parent
        local C = g:GetChildren()
        for i=1, #C do
            if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart"  then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
            end
                local Y = Instance.new("Weld")
                Y.Part0 = hit.Parent.Head
                Y.Part1 = g.Middle
                Y.C0 = CFrame.new(0, 0, 0)
                Y.Parent = Y.Part0
        end

        local h = g:GetChildren()
        for i = 1, # h do
            if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart"  then
                h[i].Anchored = false
                h[i].CanCollide = false
            end
        end

    end
end

script.Parent.Touched:connect(onTouched)

Answer this question