RBXScriptSignal & Ternary Operators

So I’m currently trying to make a script where it prints Failed. if a part hasn’t been added in the workspace withing x seconds, in this case 10. And if a part gets added within the 10 seconds (using .ChildAdded), I want that it prints “Success”.

Actually this is a really easy stuff that could be done with conditions, but I’d like to know if there is a way to do that within one line. Here’s the scripts I wrote:

local RBXscriptSignal
RBXscriptSignal = game.Workspace.ChildAdded:Wait() and print("Success") or wait(12) and print("Failed")
print("Failed.")
print("Yield Test")

When I attempted to make it like that the :Wait() the thread was yielded and would wait until the signal gets fired. Therefore, I tried to use spawn(function() end) :

local RBXscriptSignal
RBXscriptSignal = game.Workspace.ChildAdded:Wait() and print("Success") or wait(12) and print("Failed")
print("Failed.")
print("Yield Test")

But in the following case, it doesn’t print Success when a part gets added and it only prints Failed. Any ideas or is it even possible to do it that way?

1 Like

So I just forgot I noticed to put an “end” let me try to see if it does something. Still didn’t work*

In both examples of your code, I noticed a usage of ternary operators but not anything that would return a true/truthy or false/falsy value for the ternary operator to run. From the Developer Hub, I’m reading that RBXScriptSignal:Wait() returns what was fired to the signal which (correct me if I’m wrong) shouldn’t be a truthy or a falsy value. Perhaps you want something like ~= nil?

In your case here it may even be simpler for you to just use the :Connect() function instead of a :Wait() as you could set up more conditions on the Connect function as compared to having a Wait function. Keep in mind that every time someone joins, the CharacterAdded event will also fire.

Edit: Also, your ternary operator usage seems to be straying farther off after you edited your original message

1 Like

You can’t use a ternary operator with RBXScriptSignal:Wait(). The thread will pause until the signal fires, so naturally a secondary condition won’t run until the signal fires either. The second operand would be pointless in the first place because the first one would return a truthy value. If it doesn’t, the script would pause for wait(n) which is probably not what you want. That’ll just prolong the thread more.

You can make use of pseudothreading to run two pseudothreads side by side, with the addition of a boolean and a proxy event. Using this, you can check which one fires first and handles that case. The pseudothreads are throwaways so don’t worry if they’re hung.

Messy and crude example of something I’ve never tested before. I wrote this off the bat and am speaking from what I know, not from what I’ve had experience with or what’s closest to “fact”.

local eProxyEvent = Instance.new("BindableEvent")
local ProxyEvent = eProxyEvent.Event
local hasFired = false

do
    spawn(function ()
        local addedChild = workspace.ChildAdded:Wait()
        if not hasFired then
            hasFired = true
            eProxyEvent:Fire("Success", addedChild)
        end
    end)

    delay(12, function()
        if not hasFired then
            hasFired = true
            eProxyEvent:Fire("Failed")
        end
    end)
end

local result = ProxyEvent:Wait()
print(result, "- what happened after the signal fired.")

Make sure to destroy the BindableEvent and clean up variables to prevent a memory leak.

3 Likes

Anything besides nil and false is a truthy value.

2 Likes