Ad
Log in to vote
1

how do i can use the best way to wait for something happen?

Asked by 27 days ago

im making some script, one of then is "if race == X" then change body, but its only for certain races, theres is a lot and i dont do it for almost half of them, so i wanted a way to do a "wait until race is X" i just did if characterAdded and then later "if player.Race == X" then do it, but sometimes my race was this "X" and dont worked, i needed to reset for it work, so i thought in use "wait(3)" or "repeat wait() until plr.Race=="X" ", something like this, but it a good way? wts the better use to it?

another use for it is, each race has some skills and depending on the race i will give those skills so i thought in use this:

repeat wait() until race.Value ~= nil or race.Value ~=""

and then do the "skillX:clone()" in starterpack, wt u guys think, its good?

2 answers

Log in to vote
0
Answered by 27 days ago
Edited 27 days ago

I think using a coroutine is better. Example:

local thread = coroutine.create(function()
    -- code
end)
if race.Value ~= nil or race.Value ~= "" then
    coroutine.resume(thread)
end
0
but i want to it to do 1 time, like "if your race is X, ill give u this skill" and its it, i dont want a loop inside, idk if while is a better choice in this case Ariirael 15 — 27d
0
then i guess using a coroutine is better then, i'll update soon User#47934 5 — 27d
0
updated User#47934 5 — 27d
0
so when "race~= null" it will run that code block? Ariirael 15 — 27d
View all comments (5 more)
0
yes and only once User#47934 5 — 27d
0
ill try then, thx Ariirael 15 — 27d
0
Coroutines is a MUCH worse solution, also you shouldn't be using "or" there, regardless if it is "" or nil, it will run because the other conditional contradicts the former and latter. On top of this, you could substitute it for a function that would basically make this serve no purpose greatneil80 2607 — 27d
0
repeat wait() until "something" is better? i want something for just: Ariirael 15 — 27d
0
repeat wait() until "something" is better? i want something for just: when i join, ill get a race and then it'll do and give me things, wt would be the better option for "wait until race is not nil and then do these things"? Ariirael 15 — 27d
Ad
Log in to vote
0
Answered by
Leamir 3088 Moderation Voter Community Moderator
25 days ago
Edited 25 days ago

Hello, Ariirael!

The way I would do it is by using

repeat race.Changed:Wait() until race.Value ~= nil or race.Value ~=""

That code simply Waits for changes on the value of race, and does so in a loop.

Therefore, your code isn't using too much CPU time when its just waiting for a change in the race value.

However, if you set the value before starting the script, it could yield until you change it again. You can solve by using a while loop.


Useful links:

Loops


If this answers your question, please mark this as the "Accepted Answer"

0
oh, my script now is yielding i think, when i join nothing happens, but when i reset all my scripts work, almost half are "race scripts", so this while would help me u think? Ariirael 15 — 25d
0
try `while race.Value == nil or race.Value == "" do race.Changed:Wait() end`, that should stop the yelding Leamir 3088 — 24d

Answer this question