Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I move a part to a player's leg?

Hello. I am trying to make a game like Changed and I am stumped on how to move an item to a player's body part. Here is my script.

local puddle = script.Parent

puddle.Touched:Connect(function(hit)
	if hit:WaitForChild('Humanoid') then
	local rightLegLatex = Instance.new('Part')
	local rightLegLatexWeld = Instance.new('Weld')
	rightLegLatex.Size = Vector3.new(1, 2, 1)
		rightLegLatex.Parent = hit
		rightLegLatex.Position -- need help here
	rightLegLatexWeld.Part0 = hit.RightLeg
	rightLegLatexWeld.Part1 = rightLegLatex
	end
end)

1 comment

0

I may be wrong but i think that if you'll put rightLegLatex.Position = Vector3.new(hit.Parent.RightLeg.Position) -- you can change RightLeg to LeftLeg And also im not sure if it will work but im just saying that this may help ;)

1 answer

✓ Accepted answer

Ok, so heres what you need to do, you will need their right leg's position. I noticed that there was a big error in your script, hit is the part that touched the part; lets say that the left leg touched the part, hit:WaitForChild("Humanoid") would mean LeftLeg:WaitForChild("Humanoid") there will be no humanoid within someone's left leg. The solution to this is to get LeftLeg's parent, which is the actual Character. Once you are at the character, you can find their humanoid.

(you might want to add a debounce to this script to keep it from running over and over again)

debounce: https://developer.roblox.com/en-us/articles/Debounce

old script:

local puddle = script.Parent

puddle.Touched:Connect(function(hit) -- there is no problem here
    if hit:WaitForChild('Humanoid') then -- you will have to do "hit.Parent:FindFirstChild("Humanoid")  FindFirstChild will just look for it, if it does not find it, the function will return nil; I did that because WaitForChild will just wait there forever, if it touched the baseplate, it will wait forever to try to get humanoid from the baseplate.
    local rightLegLatex = Instance.new('Part')
    local rightLegLatexWeld = Instance.new('Weld')
    rightLegLatex.Size = Vector3.new(1, 2, 1)
        rightLegLatex.Parent = hit
        rightLegLatex.Position -- you have to set the position to something
    rightLegLatexWeld.Part0 = hit.RightLeg
    rightLegLatexWeld.Part1 = rightLegLatex
    end
end)

There was no need for a position anyways, I just added that to let you know how to add position in the future, The weld already sets the position to the leg. Anyways, heres what I did

New Script:

local puddle = script.Parent
local debounce = false

puddle.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")-- use FindFirstChild to avoid infinite yield
	if humanoid ~= nil and debounce == false then -- checks if it is a player, and also checks to see if deboudce is false
		local findExistingLatex = hit.Parent:FindFirstChild("RightLegLatex", true) -- I used this to prevent multiple latex to be added on the player
		debounce = true -- setting the debounce to true so the player cannot touch it multiple times really fast
		if findExistingLatex == nil then -- nil means nothing, if it finds nothing latex related, it will add a latex
			local rightLegLatex = Instance.new("Part")
			local rightLegLatexWeld = Instance.new("Weld")
			local rightLeg = hit.Parent:FindFirstChild("Right Leg")-- finding the right leg from hit
			rightLegLatex.Name = "RightLegLatex"
			rightLegLatex.Size = Vector3.new(1, 2, 1)
			rightLegLatex.Parent = hit
			rightLegLatex.Position = rightLeg.Position -- There was no need for position, just keep it as future refrence.
			rightLegLatexWeld.Part0 = rightLeg
			rightLegLatexWeld.Part1 = rightLegLatex
			rightLegLatexWeld.Parent = rightLegLatex
			wait(1)
			debounce = false -- setting the debounce to false so the another player can touch it again. 
		end
	end
end)

I really hope this is not that confusing ^-^

This discussion is preserved as it was. New answers, comments, and votes are closed.