Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

9 comments

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

0

then i guess using a coroutine is better then, i'll update soon

0

updated

0

so when "race~= null" it will run that code block?

0

yes and only once

0

ill try then, thx

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

0

repeat wait() until "something" is better? i want something for just:

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"?

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"

2 comments

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?

0

try while race.Value == nil or race.Value == "" do race.Changed:Wait() end, that should stop the yelding

This discussion is preserved as it was. New answers, comments, and votes are closed.