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
7 years ago

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

01script.Parent.Touched:connect(function(part)
02    local player = game.Players:GetPlayerFromCharacter(part.Parent)
03    if player then
04        script.Parent.Parent = player.Character.Torso
05        script.Parent.Anchored = false
06        local weld = Instance.new("Weld", player.Character.Torso)
07        weld.Part0 = player.Character.Torso
08        weld.C0 = player.Character.Torso.CFrame
09        weld.Part1 = script.Parent
10        weld.C1 = script.Parent.CFrame
11        script.Disabled = true
12    end
13end)

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:

01script.Parent.Touched:connect(function(part)
02    local player = game.Players:GetPlayerFromCharacter(part.Parent)
03    if player then
04        script.Parent.CFrame = player.Character.Torso.CFrame * CFrame.new(0, 30, 0)
05        script.Parent.Parent = player.Character.Torso
06        script.Parent.Anchored = false
07        local weld = Instance.new("Weld", player.Character.Torso)
08        weld.Part0 = player.Character.Torso
09        weld.C0 = player.Character.Torso.CFrame
10        weld.Part1 = script.Parent
11        weld.C1 = script.Parent.CFrame
12        script.Disabled = true
13    end
14end)

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 — 7y

1 answer

Log in to vote
1
Answered by 7 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

01local part = script.Parent; -- Variable for part since you use it so much.
02local players = game:GetService("Players"); -- Getting the service is bette just in-case you rename the objects in workspace.
03 
04part.Touched:Connect(function(obj) -- Capital 'C' for :Connect() because lower case is deprecated.
05    local char = obj.Parent; -- Character.
06    local player = players:GetPlayerFromCharacter(char); -- Player.
07    if player and char then -- If the player and character are there then.
08 
09        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.
10 
11        part.Parent = torso;
12        part.Anchored = false
13 
14        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.
15        weld.Part0 = torso;
View all 21 lines...

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

1local p0 = weld.Part0;
2local p1 = weld.Part1;
3local temp = p0.CFrame;
4local c0 = p0.CFrame:inverse() * temp;
5local c1 = p1.CFrame:inverse() * temp;
6 
7weld.C0 = c0;
8weld.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