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

Issues with welding on to the character?

Asked by
Griffi0n 315 Moderation Voter
6 years ago

I'm welding a part on to the player character's torso:

script.Parent.Touched:connect(function(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
    if player then
        script.Parent.Parent = player.Character.Torso
        script.Parent.Anchored = false
        local weld = Instance.new("Weld", player.Character.Torso)
        weld.Part0 = player.Character.Torso
        weld.C0 = player.Character.Torso.CFrame
        weld.Part1 = script.Parent
        weld.C1 = script.Parent.CFrame
        script.Disabled = true
    end
end)

It works but the part ends up under the player But when I try to move the part's CFrame to a different location on the player:

script.Parent.Touched:connect(function(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
    if player then
        script.Parent.CFrame = player.Character.Torso.CFrame * CFrame.new(0, 30, 0)
        script.Parent.Parent = player.Character.Torso
        script.Parent.Anchored = false
        local weld = Instance.new("Weld", player.Character.Torso)
        weld.Part0 = player.Character.Torso
        weld.C0 = player.Character.Torso.CFrame
        weld.Part1 = script.Parent
        weld.C1 = script.Parent.CFrame
        script.Disabled = true
    end
end)

All it does is move the player up and not change the CFrame of the part.

0
Don't set the C0 and C1 values. However,if you wanted to weld the part to the torso and still keep its relative CFrame, you put: weld.C0 = player.Character.Torso.CFrame:inverse() * script.Parent.CFrame UgOsMiLy 1074 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

Hey Griffi0n,

So, I tried your script in my studio, and the problem was simple. The problem was that you set the weld's C0 and C1 properties. This is not necessary since you just want it to go to the Part0's CFrame. If you wanted it to offset from the Part0's CFrame, then you'd have to set the C0 and C1 values. Anyways, I also made some enhancements to your scripts and I'll show you the script below:

Script with enhancements and fix

local part = script.Parent; -- Variable for part since you use it so much.
local players = game:GetService("Players"); -- Getting the service is bette just in-case you rename the objects in workspace.

part.Touched:Connect(function(obj) -- Capital 'C' for :Connect() because lower case is deprecated.
    local char = obj.Parent; -- Character.
    local player = players:GetPlayerFromCharacter(char); -- Player.
    if player and char then -- If the player and character are there then.

        local torso = char:WaitForChild("Torso"); -- Declare variable for torso because you use it a lot. :WaitForChild() is good to use because it makes the game compatible for StreamingEnabled and reduces lag.

        part.Parent = torso;
        part.Anchored = false 

        local weld = Instance.new("Weld") -- Don't set parent in the same parenthesis as the Instance.new() because it increases latency, making it less efficient.
        weld.Part0 = torso;
        weld.Part1 = part;
        weld.Parent = torso; -- Setting the parent at the end of the weld is better because it allows for all the C0 or C1 updates you may have in the future in this script take effect before it decides where to go.

        script.Disabled = true;
    end
end)

If you want to move the part, you will need to make all changes through the weld's C0 or C1. If you want to keep the part in the same position that it was touched, there's a specific algorithm for that. Here, let me demonstrate below:

How to keep part's position the same after welding

local p0 = weld.Part0;
local p1 = weld.Part1;
local temp = p0.CFrame;
local c0 = p0.CFrame:inverse() * temp;
local c1 = p1.CFrame:inverse() * temp;

weld.C0 = c0;
weld.C1 = c1;

Well, I hope I helped, if you want to read more on C0s and C1s, here is where you can learn about them. Have a nice day.

Thanks,

~~ KingLoneCat

Ad

Answer this question