I think you’d have more luck with a BindableEvent
, rather than a BindableFunction
. In other words, I think @DamSam127 has your solution (so you should mark his post as such rather than mine if it does end up being the solution)
If you need the data from the event for some reason, I suppose you could spawn a new thread, in which case I’d suggest using the (kind of) new task.spawn
function over coroutine.wrap
.
For example:
task.spawn(function()
gamemodeVoted = voteFunction:Invoke()
print(gamemodeVoted)
end)
for i = 30, 0, -1 do
status.Value = "A new round is starting in: "..i
wait(1)
end
The code after spawn starts running when Invoke is called, after the BindableFunction
's function yields or returns.
Also, as an extra bit of info for you or future readers… A key thing to note about how threads work, because it can be important to how your code works, threads actually don’t run at the same time as eachother at all in Roblox. Only one thread can be running at once, and other threads (and Roblox) can only continue running once it yields or ends.
That’s why an infinite loop like while true do
will cause Roblox to freeze. The thread with the loop never ends or yields to other code, so it keeps running forever.
You could visualize it like a busy road. Cars coming onto it are yielding to the other cars until there is an opening (when yielding code can start running again). Then, some cars can exit that road to get gas (the thread is yielding), or go home because they’re done driving (the thread is finished running).
When you spawn a thread, or fire an event, you make your current thread yield, and then the new code you are spawning starts running. So, with the traffic analogy you could think about it like changing a traffic light, stopping your current code and letting your spawned code go.