Move object until not clicked

Again, I am making a 2048 test game and I ran into a problem when making a cube dropper. I am trying to make the dropper move but clicking a screen button and when I release the mouse button it stops, but this happens

here is the script.

local Button = script.Parent
local Dind = game.Workspace.Drops.DropIndicator
local Box = game.Workspace.Drops.Box

Button.MouseButton1Down:Connect(function()
   repeat
      Dind.Position = Vector3.new(Dind.Position.X, Dind.Position.Y, Dind.Position.Z + .1)
      wait(.01)
   until Button.MouseButton1Up == true
end)
1 Like

Have you tried looking at the output?

I don’t think Button.MouseButton1Up == true even works, but I’m not sure. MouseButton1Up is an event so I don’t think it would be a bool.

2 Likes

I have tried it with and without the == true but with is the only one that actually makes it move.

Hmm, I’m not sure then. Look at the output and see if there’s any errors.

Nope, there is no errors or warnings, I made a print function that will print when its up and it doesn’t even run.

You should use GuiButton | Roblox Creator Documentation as an event for when the player stops pressing the GUI button.

I can’t test it unfortunately, but try this

local Button = script.Parent
local Dind = game.Workspace.Drops.DropIndicator
local Box = game.Workspace.Drops.Box
local down = false

Button.MouseButton1Down:Connect(function()
	down = true
	
	local function upDetector()
		Button.MouseButton1Up:connect(function()
			down = false
			coroutine.yield()
		end)
	end
	
	local upCoroutine = coroutine.create(upDetector)
	coroutine.resume(upCoroutine)
	
	while down do
		Dind.Position = Vector3.new(Dind.Position.X, Dind.Position.Y, Dind.Position.Z + .1)
		wait(.01)
	end

end)

Wow, that worked. Thank you so much for the help :blush:

1 Like

No problem! Sorry for the late reply, I’m only just getting home. Good luck on your game :happy1:

Also, here’s an article on coroutines, they can really come in handy sometimes and are, I think, pretty underused.

1 Like