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

How i can use wait function in running while function?

a loaded value is not changing how to fix it?

wait(game.IsLoaded)
local obj = script.Parent.Backround.GIF
local xoffset = 0
local yoffset = 0
local loaded = false

local function loadingscreen(value)
	while value == false do
		wait()
		print(value)
			if xoffset == 200 and yoffset == 500 then
				xoffset = 0
				yoffset = 0

			else
				obj.ImageRectOffset = Vector2.new(xoffset,yoffset)
				if xoffset ~= 400 then
					xoffset = xoffset + 100
				else
					if xoffset == 400 then
						xoffset = 0
						yoffset = yoffset + 100
					end
				end
			end
	end
end

loadingscreen(loaded)
wait(3)
loaded = true --this loaded value is not changing

2 answers

You have two problems here. Your first problem is that the while loop inside the function is yielding everything, not just your function. There is a way around this, however, and it's called multithreading.

A thread is a sequential flow within your script; in simpler terms, a script can only run one thing at a time. Multithreading is when you have multiple threads inside one script, so you can have a loop run while other parts run as well. There are two methods to do multithreading in Roblox, and they are:

Spawn

I would not recommend using this! Spawn has a small delay before the code runs.

spawn(function()
	--do something
end)

Coroutine

Coroutines are a bit more complex than spawn(), but they are much better. You have more control when using them, and they don't have a delay each time the code inside runs. This is one way to use coroutines.

local c --declare the coroutine as nil initially so we can yield it later
c = coroutine.create(function()
	coroutine.yield(c) --this will stop the coroutine from running. you can restart it by doing coroutine.resume(c)
end)

Another way to use coroutines is by wrapping a function.

local CoolFunction = coroutine.wrap(function(argument)
	--do something, you can pass arguments through this!
end)

By wrapping a function, you lose the ability to yield and resume the coroutine, however, you can pass arguments and use it like it's a normal function.

So, how do we apply this? Let's see:

wait(game.IsLoaded)
local obj = script.Parent.Backround.GIF
local xoffset = 0
local yoffset = 0
local loaded = false

local loadingScreen = couroutine.wrap(function(value)
    while value == false do
        wait()
        print(value)
        if xoffset == 200 and yoffset == 500 then
            xoffset = 0
            yoffset = 0

        else
            obj.ImageRectOffset = Vector2.new(xoffset,yoffset)
            if xoffset ~= 400 then
                xoffset = xoffset + 100
            else
                if xoffset == 400 then
                    xoffset = 0
                    yoffset = yoffset + 100
                end
            end
        end
    end
end)

loadingscreen(loaded)
wait(3)
loaded = true 

Alright! We got it multithreaded, but what's the other problem? Well, in your function you're checking to see if the passed value is true. But since you aren't running the function again, the loop will go on forever. I suggest not passing an argument and just checking to see if loaded changes. Here's the final script:

wait(game.IsLoaded)
local obj = script.Parent.Backround.GIF
local xoffset = 0
local yoffset = 0
local loaded = false

local loadingScreen = couroutine.wrap(function()
    while not loaded do
        wait()
        print(loaded)
        if xoffset == 200 and yoffset == 500 then
            xoffset = 0
            yoffset = 0

        else
            obj.ImageRectOffset = Vector2.new(xoffset,yoffset)
            if xoffset ~= 400 then
                xoffset = xoffset + 100
            else
                if xoffset == 400 then
                    xoffset = 0
                    yoffset = yoffset + 100
                end
            end
        end
    end
end)

loadingscreen()
wait(3)
loaded = true 

I hope my answer helped you learn something new!

Here are some resources you can use to learn about multithreading:

Coroutines

Multithreading

Spawn (you'll need to scroll down a bit)

Hiya!

Might I recommend you use

repeat
	-- do stuff
until value == false

for this code? I found this method works.

Thanks!

1 comment

0

its not working for me

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