I'm welding a part on to the player character's torso:
01 | script.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.Part 0 = player.Character.Torso |
08 | weld.C 0 = player.Character.Torso.CFrame |
09 | weld.Part 1 = script.Parent |
10 | weld.C 1 = script.Parent.CFrame |
11 | script.Disabled = true |
12 | end |
13 | 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:
01 | script.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.Part 0 = player.Character.Torso |
09 | weld.C 0 = player.Character.Torso.CFrame |
10 | weld.Part 1 = script.Parent |
11 | weld.C 1 = script.Parent.CFrame |
12 | script.Disabled = true |
13 | end |
14 | end ) |
All it does is move the player up and not change the CFrame of the part.
Hey Griffi0n,
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:01 | local part = script.Parent; -- Variable for part since you use it so much. |
02 | local players = game:GetService( "Players" ); -- Getting the service is bette just in-case you rename the objects in workspace. |
03 |
04 | part.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.Part 0 = torso; |
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:1 | local p 0 = weld.Part 0 ; |
2 | local p 1 = weld.Part 1 ; |
3 | local temp = p 0. CFrame; |
4 | local c 0 = p 0. CFrame:inverse() * temp; |
5 | local c 1 = p 1. CFrame:inverse() * temp; |
6 |
7 | weld.C 0 = c 0 ; |
8 | weld.C 1 = c 1 ; |
C0
s and C1
s, here is where you can learn about them. Have a nice day.