How to loop this code?

ui.MouseEnter:Connect(function()
	script.Parent.Image = "rbxassetid://11701470780"
	wait(0.1)
	script.Parent.Image = "rbxassetid://11701476565"
end)

im making custom cursor but i need to loop it I don’t know how to do loops /:

local debounce = false
while true do
	if debounce then break end
	script.Parent.Image = "rbxassetid://11701470780"
	wait(0.1)
	script.Parent.Image = "rbxassetid://11701476565"
end

Edit: Nevermind, I see what you’re trying to do. Will update the code

Assuming that you want to make the image change while the mouse is on the image:

local run = false
ui.MouseEnter:Connect(function()
  run = true
  while run do
	script.Parent.Image = "rbxassetid://11701470780"
	wait(0.1)
	script.Parent.Image = "rbxassetid://11701476565"
  end
end)

ui.MouseLeave:Connect(function()
  run = false
end
3 Likes

like this and n o i wanne loop that code on the post LOOP bro.

local ui = script.Parent.Parent.TextButton

ui.MouseEnter:Connect(function()
	script.Parent.Image = "rbxassetid://11701470780"
	wait(0.1)
	script.Parent.Image = "rbxassetid://11701476565"
end)

ui.MouseLeave:Connect(function()
	script.Parent.Image = "rbxassetid://11701347120"
end)

ui.MouseButton1Down:Connect(function()
	script.Parent.Image = "rbxassetid://11701412654"
end)

Will this work? (I wrote it fast so it probably won’t)

local ui = script.Parent.Parent.TextButton
local mouseToggle = false


ui.MouseEnter:Connect(function()
	mouseToggle = true
	repeat 
		script.Parent.Image = "rbxassetid://11701470780"
		wait(0.1)
		script.Parent.Image = "rbxassetid://11701476565"
	until mouseToggle == false
	
end)

ui.MouseLeave:Connect(function()
	script.Parent.Image = "rbxassetid://11701347120"
	mouseToggle = false
end)

ui.MouseButton1Down:Connect(function()
	script.Parent.Image = "rbxassetid://11701412654"
	mouseToggle = false
end)

image
when im not touching button it shows this /: isnt normal cursor normaly it shows normal cursor maybe of the loop?

Hmmmm Try this:

ui.MouseEnter:Connect(function()
	mouseToggle = true
	repeat 
		script.Parent.Image = "rbxassetid://11701470780"
		wait(0.1)
		script.Parent.Image = "rbxassetid://11701476565"
	until mouseToggle == false
	script.Parent.Image = "rbxassetid://11701347120"
end)

ui.MouseLeave:Connect(function()
	script.Parent.Image = "rbxassetid://11701347120"
	mouseToggle = false
end)

ui.MouseButton1Down:Connect(function()
	script.Parent.Image = "rbxassetid://11701412654"
	mouseToggle = false
end)
local isEntered = false

ui.MouseEnter:Connect(function()
    isEntered = true
end)

ui.MouseLeave:Connect(function()
    isEntered = false
end)

-- Doing task.wait() to prevent script exhaustion. Not sure of any other way
while task.wait() do
    if isEntered then
        script.Parent.Image = "rbxassetid://11701470780"
        task.wait(0.1)
        script.Parent.Image = "rbxassetid://11701476565"
    else
        script.Parent.Image = "rbxassetid://11701470780" -- Might be incorrect
    end
end

This should do exactly what you want. This code has a variable, that when the “ui” is entered, it is changed to true. Vice versa, when the cursor leaves, it’s changed to false. In a while true do loop, it does what your old code did.

1 Like

Yeah but i changed the code a bit i wanted it to able to do click image
image
it did not show this image here

local isEntered = false

ui.MouseEnter:Connect(function()
	isEntered = true
end)

ui.MouseLeave:Connect(function()
	isEntered = false
end)

-- Doing task.wait() to prevent script exhaustion. Not sure of any other way
while task.wait() do
	if isEntered then
		script.Parent.Image = "rbxassetid://11701470780"
		task.wait(0.2)
		script.Parent.Image = "rbxassetid://11701476565"
	else
		script.Parent.Image = "rbxassetid://11701347120" -- Might be incorrect
	end
end

ui.MouseButton1Down:Connect(function()
	script.Parent.Image = "rbxassetid://11701412654"
	isEntered = false
end)

image

here’s a image ingame

when I left down mouse it doesn’t even do I don’t know how I can make it stop can you help?

It’s not the way I would do it, but using your code maybe try this?:

local isEntered = false;
local isDown = false;

ui.MouseEnter:Connect(function()
	isEntered = true;
end)

ui.MouseLeave:Connect(function()
	isEntered = false;
	isDown = false;
end)

ui.MouseButton1Down:Connect(function()
	isDown = true;
end)

ui.MouseButton1Up:Connect(function()
	isDown = false;
end)

-- Doing task.wait() to prevent script exhaustion. Not sure of any other way
while task.wait() do
	
	if isEntered == true and isDown == true then
		ui.Image = "rbxassetid://11701412654";
	elseif isEntered == true then
		ui.Image = "rbxassetid://11701470780";
		task.wait(0.2);
		ui.Image = "rbxassetid://11701476565";
	else
		ui.Image = "rbxassetid://11701347120";
	end
	
end
1 Like

u know what it still is kinda buggy ITS mapfriend
image
go type on yt or itch . io its a game horror I’m remaking it, like someone made no player online here take my game file
mapfriend.rbxl (40.6 KB)

Okay try this:

local ui = script.Parent.Parent.TextButton;
local delayTime = 0;
local isDown = false;
local isStop = false;
local flipflop = 1;

ui.MouseEnter:Connect(function()
	delayTime = 0.2;
	isStop = false;
end)

ui.MouseLeave:Connect(function()
	delayTime = 0;
	isDown = false;
end)

ui.MouseButton1Down:Connect(function()
	isDown = true;
end)

ui.MouseButton1Up:Connect(function()
	isDown = false;
end)

function flipFlop(flag)
	
	if flag == 1 then
		script.Parent.Image = "rbxassetid://11701470780";
	else
		script.Parent.Image = "rbxassetid://11701476565";
	end
	
end

-- Doing task.wait() to prevent script exhaustion. Not sure of any other way
while task.wait(delayTime) do
	
	-- flag to stop checking this logic under reset condition (i.e. not inside button)
	if isStop == true then
		continue
	end
	
	-- if mouse button is pressed
	if isDown == true then
		script.Parent.Image = "rbxassetid://11701412654";
		continue
	end
	
	-- mouse entered but button not pressed
	if delayTime ~= 0 then
		
		flipFlop(flipflop);
		flipflop*=-1;
		
	-- default behaviour, hit once due to isStop flag
	else
		
		script.Parent.Image = "rbxassetid://11701347120";
		isStop = true;
		
	end

end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.