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

How do I get this weld code to work in a function?

Asked by 6 years ago

So, I had some code that is meant to weld an object to the player whenever they touch it, I tried to modify an older code so that it is easier to modify for each object I need to weld, but when I touch the object, it doesn't do anything.

Here is the code:

function WeldEnable (Prt0, Prt1, x, y, z)
    Prt1.CFrame = Prt0.CFrame * CFrame.new(x, y, z)
    weld = Instance.new("Weld")
    weld.Part0 = Prt0
    weld.C0 = Prt0.CFrame:Inverse()
    weld.Part1 = Prt1
    weld.C1 = Prt1.CFrame:Inverse()
    weld.Parent = Prt1
    weld.Parent.Anchored = false
end

Target = workspace.CoolOlivie057
Targetpart = "LeftLowerLeg"

script.Parent.Touched:Connect(function(hit)
    humanoid = hit.Parent:findFirstChild("Humanoid")
    if humanoid then
        if script.Parent:FindFirstChild("weld") == nil then
            WeldEnable(Target:FindFirstChild(Targetpart), script.Parent, 0, -0.0965, 0)
        end
    end
end)

Here is the code before it was modified:

script.Parent.Touched:Connect(function(hit)
    humanoid = hit.Parent:findFirstChild("Humanoid")
    if humanoid then
        if script.Parent:FindFirstChild("weld") == nil then
            leftLowerLeg = hit.Parent.LeftLowerLeg
            script.Parent.CFrame = leftLowerLeg.CFrame * CFrame.new(0, -0.0965, 0)
            weld = Instance.new("Weld")
            weld.Part0 = leftLowerLeg
            weld.C0 = leftLowerLeg.CFrame:Inverse()
            weld.Part1 = script.Parent
            weld.C1 = script.Parent.CFrame:Inverse()
            weld.Parent = script.Parent
            weld.Parent.Anchored = false
        end
    end
end)

BTW, the unmodified code works.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
--you did not define Prt0 and Prt1 in the script
-- for example l
local Prt0 = script.Parent
local Prt1 = Script.Parent.Parent

--so you can't say
 weld.Part0 = Prt0
0
K thanks, I thought it was like in other coding languages where I would declare them in the brackets when I declare the function CoolOlivie057 45 — 6y
Ad

Answer this question