Brody Critchlow
Brody Critchlow

Reputation: 63

Is there a reason why this code is not outputting a worth

Problem

Hello, StackOverflow community! I am working on this Lua game, and I was testing to see if it would change the text on my TextLabel to the Bitcoins current worth, I was utterly disappointed when nothing showed up.

I have tried to do research on Google, and my code seems to be just right.

Code

Change = false
updated = false 
while Change[true] do   --While change = true do
    worth = math.random(1,4500) --Pick random number
    print('Working!') --Say its working
    Updated = true --Change the updated local var.
end --Ending while loop

script.Parent.TextLabel.Text.Text = 'Bitcoin is currently worth: '  .. worth 
--Going to the Text, and changing in to a New worth.
while Updated[false] do --While updated = false do
    wait(180) --Wait
Change = true --After waits 3 minutes it makes an event trigger
end -- Ending while loop
wait(180) --Wait
Updated = false --Reseting Script.

I expect the output on the Label to be a random number.

Upvotes: 2

Views: 88

Answers (2)

BJ Black
BJ Black

Reputation: 2521

I can't really speak to roblox, but there are a couple of obvious problems with your code:

  1. Case

You have confusion between capitalized ("Updated", "Change") and lowercase ("updated", "change" [in commented while statement]), which will fail. See, for example:

bj@bj-lt:~$ lua
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> Updated = true
> print(Updated)
true
> print(updated)
nil

So be super-careful about what identifiers you capitalize. In general, most programmers leave variables like that in all-lowercase (or sometimes things like camelCase). I suppose there might be some oddball lua runtime out there that is case-insensitive, but I don't know of one.

  1. Type misuse.

Updated is a boolean (a true/false value), so the syntax:

while Change[true] do

...is invalid. See:

> if Updated[true] then
>> print("foo")
>> end
stdin:1: attempt to index global 'Updated' (a boolean value)
stack traceback:
    stdin:1: in main chunk
    [C]: in ?

Note also that the "While change == true do" is also wrong because of case ("While" is not valid lua, but "while" is).

Lastly:

  1. Lack of threading.

You have basically two different things that you're trying to do at once, namely randomly change the "worth" variable as fast as possible (it's in a loop) and see a set a label to match it (it looks like you probably want it to change constantly). This requires two threads of operation (one to change worth and another to read it and stick it on the label). You've written this like you're assuming you have a spreadsheet or something and that. What your code is actually doing is:

  • Setting some variables
  • Updating worth indefinitely, printing 'Working!' a bunch, and...
  • Never stopping

The rest of the code never runs, because the rest of the code isn't in a background thread (basically the first bit monopolizes the runtime and never yields to everything else).

Lastly, even if the top code was running in the background, you only set the Text label one-time to exactly "Bitcoin is currently worth: 3456" (or some similar number) one time. The way this is written there won't be any updates thereafter (and, if it runs once before the other thread has warmed up, it might not be set to anything useful at all).

My guess is that your runtime is spitting out errors left and right due to the identifier problems and/or is running in a tight infinite loop and never actually getting to the label refresh logic.

Upvotes: 3

Kylaaa
Kylaaa

Reputation: 7188

BJ Black has given an excellent description of the issues with the syntax, so I'll try to cover the Roblox piece of this. In order for this kind of thing to work properly in a Roblox game, here are some assumptions to double check :

  • Since we are working with a TextLabel, is it inside a ScreenGui? Or a SurfaceGui?
    • If it's in a ScreenGui, make sure that ScreenGui is in StarterGui, and is this code in a LocalScript
    • If it's in a SurfaceGui, make sure that SurfaceGui is adorning a Part and this code is in a Script

After you checked all those pieces, maybe this is closer to what you were thinking :

-- define the variables we're working with
local textLabel = script.Parent.TextLabel 
local worth = 0

-- create an infinite loop
spawn(function()
    while true do

        --Pick random number
        worth = math.random(1,4500) 

        -- update the text of the label with the new worth
        textLabel.Text = string.format("Bitcoin is currently worth: %d", worth)

        -- wait for 3 minutes, then loop
        wait(180)
    end
end)

I removed Updated and Changed because all they were doing was deciding whether or not to change the value. The flow of your loop was:

  1. do nothing and display an undefined number. Wait 3 minutes
  2. update the number, display it, wait 6 minutes
  3. repeat 1 and 2.

So hopefully this is a little clearer and closer to what you were thinking.

Upvotes: 1

Related Questions