Hi all, I’m trying to make a round system and i was wondering how i could wait for an event? I looked up the Binabled Event documentation and it said to use lua Event:Wait() but that didn’t work. Thanks
When you mean wait for an event, are you iterating through a loop and want it to break when the event is called or wait for it to be fired?
Well yeah i want the function to return something only after the event is fired
To be more precise about my situation i have a event once all players have selected a class and i want to wait for that function to be fired before i start another round
I’m not exactly sure what you mean, but when a event is fired, you can receive the information this way:
local Event = game.ReplicatedStorage:WaitForChild("Event")
Event.Event:Connect(function()
--Run the code..
end)
Okay yeah thank you for the help!
When you’re talking about the classes, you could have a count, for how many times the event has been called, if the count reaches the amount of players in the game you can continue.
Make sure you call the Bindable Event, it won’t get triggered on it’s own.
Use BindableEvent:Fire()
and in your function use the function that you listed above BindableEvent:Wait()
About your situation, BindableEvents isn’t the best option, i suggest doing the following:
local Players = game:GetService("Players") -- Players Service
local MAX_PLAYERS = Players.MaxPlayers -- Max place players count
local PLAYERS_NEEDED = MAX_PLAYERS -- How much players needed to start the round
local RoundStarted = false -- Is round started
local RoundDuration = 60 -- Seconds
function StartRound()
-- Start the round
RoundStarted = true
-- Your custom code for the round here
-- Finish the round
wait(RoundDuration)
RoundStarted = false
end
function StartRoundIfPossible()
if not RoundStarted then
-- Getting a table of players and getting it's elements count using the '#' operator
local PlayersCount = #Players:GetPlayers()
if PlayersCount >= PLAYERS_NEEDED then
StartRound()
end
end
end
Players.PlayerAdded:Connect(StartRoundIfPossible)
Although that works, I think he’s waiting for them to “select a kit”, so that wouldn’t work.
Oh yea, i didn’t notice that 30 characters
What could have been done, is having a boolean value when they have selected their kit,
and iterating through each player checking if they all have that value being true but I guess it’s too late now.
iirc BindableEvent:Wait()
just yields the current thread until a result is returned, so you can still use it. This is how the below line works.
local character = player.Character or player.CharacterAdded:Wait()
It checks if the character exists, and if it doesn’t, it uses :Wait()
to wait until it’s added and then gets the returned result from :Wait()
.
Of course, you can also do this using Connect:
local character
player.CharacterAdded:Connect(function(char)
character = char
-- continue code
end)
but using :Wait() just looks cleaner imo.