Assert() without an error message?

For coroutines: I’ve considered this as a solution, but unfortunately it’s rather chunky (hard to read and takes up a lot of space), and I’m not sure it’s too performant either. My animations mostly take place in a modulescript, so I’m not sure how coroutines would line up as well

For task.spawn: I couldn’t figure out how to cancel the thread that ran the (replica-)assert function

Can’t you write a function that checks if the inputted value is nil?

local function checkVal(value)
   if value then
      return true
   end
end

This doesn’t cancel the thread that ran checkVal, so if you even wanted to cancel the thread then you’d have to do a conditional statement (:fearful:)

Also, the conditional statement (to check if animator exists) would do this for you

Then instead of returning any value, use task.cancel like @2jammers stated.

h o w ?

I’ve attempted to use it, but this is what the code looks like:

function fakeAssert(any: any)
    if not any then 
        task.cancel() 
    end -- cancel what??
end

function myAnimation(character)
   fakeAssert(findAnimator(character)) -- find animator just finds the animator
end

How does fakeAssert terminate the myAnimation thread when it returns false. I’ve attempted to figure out how to specifically cancel it, but couldn’t find out how to actually cancel the myAnimation thread that’s being ran

Did you try task.cancel(script)? This should cancel the current thread without returning an error, as @2jammers stated.

I assumed task.cancel(script) would completely terminate the whole script (which seems a bit problematic), but I’ll try it

Error: invalid argument #1 to 'cancel' (thread expected, got Instance)

I guess your best bet is to disable the script then. Just know that you can’t re-enable it from inside the same script, only disable.

Very epic solution

I’ll probably just end up making a feature request to see what happens, having an assert method without cluttering up the output would be really nice

(Update:… feature requests are beyond my trust level, I guess I’ll have to deal with conditional statements for now…
update 2: aaa it’s all boilerplate it’s like an actual nightmare noooooo)

If this is genuinely a problem for you, you should switch to OOP or implement functions rather than copying and pasting code.

2 Likes

Use pcall at the start of your script to prevent from any type of errors

Example :

local suc,err = pcall(function()
if game.NotLighting == true then
-- Your script
end
end)

you can check why it errors or if its successful and has no errors at all by doing this

Example :

local suc,err = pcall(function()
if game.NotLighting == true then
-- Your script
end
end)
print(suc) -- if its true then its successful otherwise it has errors
print(err) -- this prints the reason of the error if its nil then it means there is no error

this is exactly what this post is about, a function to assert without passing an error is the same as doing if condition then return end

Thank you @hoontee for the solution!!:

function demand(condition: any): any
	if condition then
		return condition
	else
		task.defer(coroutine.close, coroutine.running())
		coroutine.yield(coroutine.running())
		return nil
	end
end

demand(2 == 1)
print("test") -- "test" will not be printed, because the demand statement above returned false, which terminated the thread -- without an error!
3 Likes

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