Stopping a function at midpoint?

So I have a function thats called errorText(text). It basically changes a gui’s text to the parameter “text”, and then slowly fades out ( slowly changing transparency to 1 ). Here is the script:

function errorText(text)
	local textLabel = player.PlayerGui.ErrorMessages.TextLabel
	textLabel.Visible = false
	textLabel.Visible = true
	textLabel.Text = text
	task.wait(0.8)
	for loop = 0, 1, 0.25 do
		textLabel.TextTransparency = loop
		task.wait(0.15)
	end
	textLabel.Visible = false
end

This does work, it does fade out after 0.8 seconds. However, if the player calls this function twice in a row, it breaks. The textlabel fades out at first, after the player called the function again, the first time the player called errorText overlaps with second time calling errorText, glitching it.
My question is:
Is there any way to check if the textlabel is visible, if it is, it pauses the last time calling the function errorText() and starts a new one? Thanks.

local debounce = false

function errorText(text)
if not debounce then
    debounce = true
	local textLabel = player.PlayerGui.ErrorMessages.TextLabel
	textLabel.Visible = false
	textLabel.Visible = true
	textLabel.Text = text
	task.wait(0.8)
	for loop = 0, 1, 0.25 do
		textLabel.TextTransparency = loop
		task.wait(0.15)
	end
	textLabel.Visible = false
   end
end

This only prevents other errorText functions being called, but I want it to be like when a new errorText is called, it cancels the last errorText immediately and does the new one ( kind of like overlap the old one )

2 Likes

Try this

function errorText(text)
	local textLabel = player.PlayerGui.ErrorMessages.TextLabel
    textLabel.TextTransparency = 0
	textLabel.Visible = true
	textLabel.Text = text
	task.wait(0.8)
	for loop = 0, 1, 0.25 do
		if textLabel.TextTransparency < loop then return end
        textLabel.TextTransparency = loop
		task.wait(0.15)
	end
	textLabel.Visible = false
end

What I said is incorrect. He wants to be able to click it again, but just overide the last one. So if transparency is going down and is at 0.5. I think he wants it to go back to 0, and overide the old function.

1 Like

yessss thats what I want, not preventing a new one…

This might help

I don’t exactly understand what it means. I don’t want a while loop? I just want the function to be called once.

Yeah I don’t know what it means either. What we can do is create a bool value, fire an event when that boolvalue changes and break the loop.

function errorText(text)
	workspace.BoolValue.Value = true
	local textLabel = player.PlayerGui.ErrorMessages.TextLabel
	textLabel.Visible = false
	textLabel.Visible = true
	textLabel.Text = text
	task.wait(0.8)
	for loop = 0, 1, 0.25 do
		textLabel.TextTransparency = loop
		task.wait(0.15)
		workspace.BoolValue:GetPropertyChangedSignal("Value"):Connect(function()
			break
		end)
		
	end
	workspace.BoolValue.Value = false
	textLabel.Visible = false
end

This may or may not work. I’m worried that changing the value at the start will cause the loop to break but we can see.

I’ll try that later ( I have a feeling that it will work ) I am currently outside.

1 Like

The break won’t work as it isn’t inside a loop. It is inside a function. You also spelt BoolValue incorrectly, so if @2kvigilanxe names the value in workspace BoolValue, it won’t work since it says BoolVlaue in the script.

Try mine but make sure you don’t change textLabel.TextTransparency in another function or you’ll get a bug.

Yeah I rushed through it. sorry

I’ve been trying to fix this for a while now, but everything I do just makes it disappear instantly and does not even fix the issue. I think this might be possible, but is very difficult. Just leave it as how it is for now, and try again in a week.

There are a lot of different ways to do this, but since @SilentFish20 suggested using .Changed I would actually just suggest encapsulating the entire function. I haven’t checked but I’m fairly sure this will reset the function every time it’s called. You would then just have to change the bool when the player calls it. If I’m wrong then since .Changed is an event you can also use :Disconnect() and then reconnect it when desired.

https://developer.roblox.com/en-us/articles/events

BoolValue:GetPropertyChangedSignal("Value"):Connect(function()
-- function
end)
1 Like

So when I disconnect the function, the whole thing breaks and starts again from the first line when the event triggers?

1 Like

You should really use Tweenservice for this.

Playing 2 tweens over the same property automatically cancels the last tween.

Not to mention the result will look smoother too.