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

How to make a part move when another part is touched by a humanoid?

I am trying to make another part move when another is touched by a humanoid, but my script isn't working. Can somebody explain why?

local Part1 = workspace.Part1
local Part2 = workspace.Part2
	 
	local function onTouch(Part2)
	if Part2.Parent:FindFirstChild("Humanoid") then 
	wait()
	for i= 1, 300 do
	Part1.Position = workspace.Part1.Position * CFrame.new(0,-0.1,0)
	wait()
	end
	for i= 1, 300 do
	Part1.Position = workspace.Part1.Position * CFrame.new(0,0.1,0)
	wait()
	end
	end
	end
	Part2.Touched:Connect(onTouch)

2 answers

local Part1 = workspace.Part1
local Part2 = workspace.Part2

Part2.Touched:Connect(function(obj)
    if obj.Parent:FindFirstChild("Humanoid") then 
   	 wait()
   	 for i= 1, 300 do
    		Part1.Position = workspace.Part1.Position * CFrame.new(0,-0.1,0)
   		 wait()
   	 end
   	 for i= 1, 300 do
   		 Part1.Position = workspace.Part1.Position * CFrame.new(0,0.1,0)
   		 wait()
    	end
    end
end)

try this

1 comment

0

You forgot the ) at the end

You are overwriting the Parameter Part2 and the Variable Part2, I suggest you change the hit parameter

local Part1 = workspace.Part1
local Part2 = workspace.Part2

local function onTouch(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then 
    	wait()
	    for i= 1, 300 do
		    Part1.Position = workspace.Part1.Position * CFrame.new(0,-0.1,0)
		    wait()
	    end
		    for i= 1, 300 do
		    Part1.Position = workspace.Part1.Position * CFrame.new(0,0.1,0)
	    wait()
	    end
	 end
    end
    Part2.Touched:Connect(onTouch)

Hope that helped !

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