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.
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: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)
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: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;
C0
s and C1
s, here is where you can learn about them. Have a nice day.