Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help with changing values in a NumberVale for a if statement in a loop, any help?

Asked by 5 years ago

I'm trying to make is so after 10 seconds it changes the value of a NumberValue into 3, changing the decal's texture. When i start the game all is well but after 10 seconds have passed the value has not changed! I have tried changing it while in testing mode but the decal's texture does not change, it just stays the same value and repeats the loop. The script:

local MushNorm = script.Parent.Value.Value

while true do

    if MushNorm < 2 then

    script.Parent.Texture = "rbxassetid://2812600930"
    wait(3)

    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812606670"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)

    else
        script.Parent.Texture = "rbxassetid://2813243667"
        end
end

wait(10)
script.Parent.Value.Value = 3

2 answers

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Using a while-loop like this is causing line 37 to never run because you don't have a break anywhere to exit the loop. This while-loop is considered infinite even if you have this if-statement. It never gets to the wait(10) or line 37 since MushNorm is always 2.

To fix this you could use spawn() to have the while-loop in a new thread to allow line 37 to actually run which makes the condition not pass:

--Consider changing "Value" object to a proper name.
--It helps in the long run when you add more stuff to the game.
local MushNorm = script.Parent.Value.Value 

spawn(function()
    while true do
        if MushNorm < 2 then
            script.Parent.Texture = "rbxassetid://2812600930"
            wait(3)
            script.Parent.Texture = "rbxassetid://2812602676"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812603913"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812605479"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812606670"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812605479"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812603913"
            wait(0.05)
            script.Parent.Texture = "rbxassetid://2812602676"
            wait(0.05)
        else
            script.Parent.Texture = "rbxassetid://2813243667"
            wait()
        end
    end
end)

wait(10)
mushNorm = 3

I didn't add a break since it seems this is for testing purposes (Assuming here). I do recommend adding a break somewhere in this code inside some condition.

If you want to know about spawn() then you can see more on it here.


Another thing you could consider is setting up your textures into a table and use some for-loops to set the Texture property to the indexed assetId.

Edit: Last imporant thing you should read up on is using wait() properly. This blog post explains it some.

0
I'm insanely new to scripting and I understand the spawn some but don't know how to properly use it. Also what do you mean about the last sentence? OneDelectableFruit 7 — 5y
0
spawn is very useful, but its not totally neccesary for what you're doing. i showed a couple other ways you could achieve what(i think) you're looking for. DinozCreates 1070 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You put it into a perpetual while loop then told it after the while loop ends, do something. So it was never able to do what you asked. If we change the value before the while loop though, it wont do what im thinking you want it to.

local MushNorm = script.Parent.Value.Value

wait(10)
script.Parent.Value.Value = 3

while true do

    if MushNorm < 2 then

    script.Parent.Texture = "rbxassetid://2812600930"
    wait(3)

    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812606670"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)

    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)

    else
        script.Parent.Texture = "rbxassetid://2813243667"
        end
end

A different way to do it. 10 seconds is roughly 3 times your while loop, so we could just run it 3 times then change our value and texture ID.

local MushNorm = script.Parent.Value -- don't use .Value in a variable to reference the actual value!

for i = 1, 3, 1 do
    script.Parent.Texture = "rbxassetid://2812600930"
    wait(3)
    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812606670"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812603913"
    wait(0.05)
    script.Parent.Texture = "rbxassetid://2812602676"
    wait(0.05)
end

MushNorm.Value = 3
script.Parent.Texture = "rbxassetid://2813243667"

This method should also work for you, im making it under the assumption that MushNorm is starting off at 0. So it will run 3 times, incrementing the value by once, then once it gets to 3 it would start running else instead.

local MushNorm = script.Parent.Value

while true do

    if MushNorm < 3 then
        script.Parent.Texture = "rbxassetid://2812600930"
    wait(3)
        script.Parent.Texture = "rbxassetid://2812602676"
        wait(0.05)
    script.Parent.Texture = "rbxassetid://2812603913"
        wait(0.05)
        script.Parent.Texture = "rbxassetid://2812605479"
        wait(0.05)
        script.Parent.Texture = "rbxassetid://2812606670"
        wait(0.05)
        script.Parent.Texture = "rbxassetid://2812605479"
    wait(0.05)
        script.Parent.Texture = "rbxassetid://2812603913"
        wait(0.05)
    script.Parent.Texture = "rbxassetid://2812602676"
        wait(0.05)
    MushNorm.Value = MushNorm.Value + 1
    else
        script.Parent.Texture = "rbxassetid://2813243667"
        end
end

Answer this question