Making function play when button is clicked

So I’m experimenting with code, the code without being a function ALREADY WORKS, I am just trying to make it so when I click the button it will work.
Here is the code I used

local cheese = script.Parent
local Button = game.Workspace.Button.ClickDetector
local dir = 1

local function moveCheese()
	while true do
		cheese.Position = cheese.Position + Vector3.new(0,1*dir,0)
		wait(.15)

		if (cheese.Position.Y > 10) then 
			dir = -1
		end

		if (cheese.Position.Y < 5) then
			dir = 1
		end
end

end
Button.ClickDetector.MouseClicked:Connect(moveCheese)

1 Like

You are trying to use Button.ClickDetector whilst in the beginning you already defined ClickDetector therefore you are asking for Button.ClickDetector.ClickDetector.MouseClicked:Connect(moveCheese) upon writing this MouseClicked isn’t something you can use to my knowledge, the fix is simply;

Button.MouseClick:Connect(moveCheese)
1 Like