Need help with ending a script in an If statement

I want to end the script if cooldown is set to true but I’ve used return but when I use that it repeats the function itself, how would I go about doing this? Basically i want to make so the script does not continue to the code below if cooldown is true

if Cooldown.Value==true then
				-- i want to end the script here if cooldown is true
					
			else
				CooldownTimer(EntityCharacter, CooldownName)
			end
2 Likes

What is the triggering event for this code? (e.g. heartbeat, renderstepped, etc)
Return is likely what you are looking for, but if the code runs again something else is doing it.

[“Red Stomp”] = {

	["Down"] = function(Data)
		local EntityCharacter = Data.EntityCharacter
		local Cooldown = Data.EntityCharacter.Cooldown:FindFirstChild("Red Stomp")
		local CooldownName = "Red Stomp"
		if  Cooldown.Value==true then
			return
				
		else
			CooldownTimer(EntityCharacter, CooldownName)
		end

I think it should be return nil not too sure. Test it out when you can.

return should do it, but if Cooldown returns nil it would run the else code.
Also, if it is being triggered from user input button down and button up will create two events.

1 Like

Ah so, return nil wouldn’t work cause it’s gonna run the else statement?

Can you send the rest of your code? It’s kind of hard to understand what’s going on when all we get is this snippet.

What I’d imagine is going on is somewhere if your code there’s a loop calling the “Down” function, so returning early just causes the function to be called again on the next loop.

If you’re looking to break out of the loop completely there’s a few ways of doing this off the top of my head that includes setting a global boolean, erroring instead of returning, or yielding the thread.

1 Like

No.
return by itself is returning nil
return nil is also returning nil
Both would return nil to the function that called it, and normally would exit the function running (so in this case RedStomp.Down)
What I meant was if the findfirstchild failed it would set cooldown as nil, thus triggering the ‘else’ part of the code.
See, @yellp1 's post, the return value probably isn’t the issue here.

A minor correction; If the FindFirstChild returned nil, or an object that wasn’t a BoolValue, it would just error rather than run the else code.
Totally see what you’re were getting at here though!

You dont actually need the if else statement at all, you just need to end the script if the value is true

Do this instead

	if Cooldown.Value == true then return end
	
	CooldownTimer(EntityCharacter, CooldownName)
	

i fixed the issue of it repeating itself but now I want to make so the code is actually able to run again when its use because players press V to toggle it, but now when the cooldown is true and the return statement is used the code dosent work again

Can you try using break, it might work.

I know it works with inside functions:

function test(parameter)
    print("1")
    break
    print("2")
end

…or in for statements:

for i = 1,5 do
    print(i)
    if i == 3 then
        break
    end
end

So see if if statements could work aswell, or try using return instead.

Use RunService, or make it so that if the cooldown is true then it will print(“Cooldown”) else, CooldownTimer(EntityCharacter, CooldownName)

ive used a return but I want the script to be able to be repeated again when a player presses a key, when I use return it terminates the whole entire script

Turning the script into a ModuleScript might help you.
Or putting that part of the script in a ModuleScript and calling it might help.

Then wrap the if statement in a function, and you can then call it whenever you need to.