The title is self explanatory but I’m working on a round system, and when a button is pressed, It fires an event, and when that event is fired, it adds a point to a table.
Why does it need to be in a loop? Why can’t you asynchronously handle it?
As @Geolio9 asked, pretty much any method of handling events except for this would be preferred. But you could set a certain variable or value somewhere whenever the event is fired, and then have your loop check that variable/value each time it runs. Or, though I REALLY don’t recommend doing this in this context, you can open a script connection from within a loop.
Again, REALLY don’t recommend it, even if you take precautions against memory leaks, unless it’s absolutely necessary based on your setup. And if that’s the case, you should really re-setup your setup.
Great points from the people above. If your system absolutely needs to wait for an event to be fired in your loop, you can use RBXScriptSignal:Wait()
.
while true do
-- start game logic
local someResponse = someEvent.Event:Wait() -- yields until the BindableEvent gets fired
-- end game logic
end
local button = script.Parent
local points = {}
local function addpoints()
local point = 10
table.insert(points, point)
end
while wait() do
button.MouseButton1Click:Connect(function()
addpoints()
end)
end
I hope this helped get an idea at least.
No, absolutely not, do not follow this advice.
- You are reconnecting the event every
wait()
deltaTime. -
MouseClick1
is not even anRBXScriptSignal
.
Interesting, That would work, But I want it to like simultaneously keep the game running, Wouldn’t that just wait until its fired? Also the plan is to make it fire numerous times, Instead of just once, I can provide a watered-down version of the script if you would like.
Then separate it from your while true do
logic (just before if you are not running it on a separate thread), or run it on a separate script.
This is what we said further up in the thread. It wouldn’t interrupt whatever you’re doing in the loop.
Okay bear with me i’m just writing logic for this.
This?
local fired = false
event:Connect(function() -- The event runs on the separate thread
fired = true
end)
while task.wait(1) do
print(fired)
end
I’m on my phone since im out oops I can’t remember the actual codes its what I came up with on the spot loll i think its MouseButton1Click then and the guy was very vague about his issue so that’s why i went with a simple wait.
Alright I was working on the logic and a bug seems to be occuring? I know its kind of unrelated but it isn’t getting the fired
Server
Events.BindableEvents.AddPoint.Event:Connect(function(player, amount)
print("caught")
table.insert(tableOfPlayers, {Name = player, amount + amount})
print(tableOfPlayers)
end)
Client
button.MouseButton1Click:Connect(function()
print('fired')
EventsFolder.AddPoint:Fire('1')
end)
He was referring to the post made by @DIREFUL_7N1F3. Also, which one isn’t getting fired? If it’s the localscript, where is your localscript parented?
Are you using a GuiButton
? Use a RemoteEvent
instead of a BindableEvent
for server-client communication.
in your title you checking for an event being fired and that loop is also a loop so then it checks every time you wait which can lead to your servers crashing, although I did this myself once and then realized after I coded my whole thing
I was only using server-client for testing as in the real game there will be like actual logic for adding points, I thought bindables were 2-way instead of just server-server or client-client.
No, bindables are strictly for server-server and client-client.
I never knew that lol, Okay I guess I can just change it later.
Another question, How can I like actually format the table? I’ not that familiar with table formatting, Rn im using it like this
table.insert(tableOfPlayers, {Name = player, points + points})
But in the output its just spamming my name a bunch
Is there a better way to format this?
- Do not use mixed tables. Either use an array (numerical indices) or a dictionary (non-numerical indices). In your
table.insert
, you are inserting a table containing the keyName
with the value ofplayer
, but a numerical entry ofpoints + points
, appending it to the first index. - Use a dictionary instead with the key
Name
and the value ofpoints
:
tableOfPlayers[player.Name] = (tableOfPlayers[player.Name] or 0) + amount
-- your table will now look like this:
{
["dupewastaken"] = 5,
["anotherPlayer"] = 10
}
-- then when you want to clear the tableOfPlayers after every round
table.clear(tableOfPlayers)