这段代码没有输出值是有原因的吗
Is there a reason why this code is not outputting a worth
问题
您好,Whosebug 社区!我正在开发这个 Lua 游戏,我正在测试它是否会将我的 TextLabel 上的文本更改为比特币当前价值,当什么都没有出现时我感到非常失望。
我已经尝试研究Google,我的代码似乎是正确的。
代码
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.
我希望 Label 上的输出是一个随机数。
我真的不能和 roblox 说话,但是你的代码有几个明显的问题:
- 案例
您混淆了大写字母("Updated"、"Change")和小写字母("updated"、"change" [in commented while statement]),这将失败。参见,例如:
bj@bj-lt:~$ lua
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> Updated = true
> print(Updated)
true
> print(updated)
nil
所以 super-careful 关于您将哪些标识符大写。通常,大多数程序员都将变量保留为 all-lowercase 中的那种(有时像驼峰式一样)。我想那里可能有一些奇怪的 lua 运行时 case-insensitive,但我不知道有一个。
- 类型误用。
Updated 是一个布尔值(true/false 值),所以语法:
while Change[true] do
...无效。参见:
> 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 ?
另请注意,"While change == true do" 也因大小写错误("While" 无效 lua,但 "while" 有效)。
最后:
- 缺少线程。
你基本上有两件不同的事情想同时做,即尽可能快地随机更改 "worth" 变量(它在循环中)并查看设置标签以匹配它(看起来您可能希望它不断变化)。这需要两个操作线程(一个更改值,另一个读取值并将其贴在标签上)。你写这个就像你假设你有一个电子表格或其他东西。您的代码实际做的是:
- 设置一些变量
- 无限更新值,打印'Working!'一堆,然后...
- 永不止步
其余代码永远不会运行,因为其余代码不在后台线程中(基本上第一位独占运行时并且永远不会屈服于其他所有内容)。
最后,即使最上面的代码在后台是 运行,您也只需将文本标签 one-time 设置为恰好 "Bitcoin is currently worth: 3456"(或某个类似的数字)一次。这样写的方式之后不会有任何更新(而且,如果它在另一个线程预热之前运行一次,它可能根本不会设置任何有用的东西)。
我的猜测是,由于标识符问题,您的运行时会左右吐出错误 and/or 运行 处于紧密的无限循环中,并且从未真正进入标签刷新逻辑。
BJ Black 对语法问题给出了很好的描述,所以我将尝试涵盖 Roblox 部分。为了让这种东西在 Roblox 游戏中正常工作,这里有一些假设需要仔细检查:
- 由于我们使用的是 TextLabel,它是否在 ScreenGui 中?还是 SurfaceGui?
- 如果它在 ScreenGui 中,请确保 ScreenGui 在 StarterGui 中,并且此代码在 LocalScript 中
- 如果它在 SurfaceGui 中,请确保 SurfaceGui 正在装饰一个 Part 和此代码
在脚本中
在你检查了所有这些之后,也许这更接近你的想法:
-- 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)
我删除了 Updated
和 Changed
,因为它们所做的只是决定是否更改值。你的循环流程是:
- 不执行任何操作并显示未定义的数字。等待 3 分钟
- 更新号码,显示,等待6分钟
- 重复 1 和 2。
所以希望这更清楚,更接近您的想法。
问题
您好,Whosebug 社区!我正在开发这个 Lua 游戏,我正在测试它是否会将我的 TextLabel 上的文本更改为比特币当前价值,当什么都没有出现时我感到非常失望。
我已经尝试研究Google,我的代码似乎是正确的。
代码
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.
我希望 Label 上的输出是一个随机数。
我真的不能和 roblox 说话,但是你的代码有几个明显的问题:
- 案例
您混淆了大写字母("Updated"、"Change")和小写字母("updated"、"change" [in commented while statement]),这将失败。参见,例如:
bj@bj-lt:~$ lua
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> Updated = true
> print(Updated)
true
> print(updated)
nil
所以 super-careful 关于您将哪些标识符大写。通常,大多数程序员都将变量保留为 all-lowercase 中的那种(有时像驼峰式一样)。我想那里可能有一些奇怪的 lua 运行时 case-insensitive,但我不知道有一个。
- 类型误用。
Updated 是一个布尔值(true/false 值),所以语法:
while Change[true] do
...无效。参见:
> 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 ?
另请注意,"While change == true do" 也因大小写错误("While" 无效 lua,但 "while" 有效)。
最后:
- 缺少线程。
你基本上有两件不同的事情想同时做,即尽可能快地随机更改 "worth" 变量(它在循环中)并查看设置标签以匹配它(看起来您可能希望它不断变化)。这需要两个操作线程(一个更改值,另一个读取值并将其贴在标签上)。你写这个就像你假设你有一个电子表格或其他东西。您的代码实际做的是:
- 设置一些变量
- 无限更新值,打印'Working!'一堆,然后...
- 永不止步
其余代码永远不会运行,因为其余代码不在后台线程中(基本上第一位独占运行时并且永远不会屈服于其他所有内容)。
最后,即使最上面的代码在后台是 运行,您也只需将文本标签 one-time 设置为恰好 "Bitcoin is currently worth: 3456"(或某个类似的数字)一次。这样写的方式之后不会有任何更新(而且,如果它在另一个线程预热之前运行一次,它可能根本不会设置任何有用的东西)。
我的猜测是,由于标识符问题,您的运行时会左右吐出错误 and/or 运行 处于紧密的无限循环中,并且从未真正进入标签刷新逻辑。
BJ Black 对语法问题给出了很好的描述,所以我将尝试涵盖 Roblox 部分。为了让这种东西在 Roblox 游戏中正常工作,这里有一些假设需要仔细检查:
- 由于我们使用的是 TextLabel,它是否在 ScreenGui 中?还是 SurfaceGui?
- 如果它在 ScreenGui 中,请确保 ScreenGui 在 StarterGui 中,并且此代码在 LocalScript 中
- 如果它在 SurfaceGui 中,请确保 SurfaceGui 正在装饰一个 Part 和此代码 在脚本中
在你检查了所有这些之后,也许这更接近你的想法:
-- 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)
我删除了 Updated
和 Changed
,因为它们所做的只是决定是否更改值。你的循环流程是:
- 不执行任何操作并显示未定义的数字。等待 3 分钟
- 更新号码,显示,等待6分钟
- 重复 1 和 2。
所以希望这更清楚,更接近您的想法。