Roblox Studio中文标题:文本值没有改变,Lua语言

我的新游戏《Opix Islands》中的交付系统令我苦苦挣扎,我相信是这个脚本导致了问题。我认为值已经正确改变了,但我不能百分之百确定。我认为问题在于文本的更改。我已经确认在GUI移动之前,文本应被更改。感谢任何帮助。

此外,我在Roblox开发者论坛上进行了研究,以检查它是否为随机功能,并且我不认为它有错。我也没有看到任何错误,但屏幕上的文本绝对没有改变。

script.Parent.ClickDetector.MouseClick:Connect(function(plr)

    script.Parent.Parent.Script.BoxPresent.Value = false
    local value = math.random(1,6)
    wait()
    local text = script.BoxUi.TextLabel.Text
    if value == 0 then
    text = "将包裹发送到:Opix推送中心"
    elseif value == 1 then
    text = "将包裹发送到:1市场街,Opix"
    elseif value == 2 then
    text = "将包裹发送到:2市场街,Opix"
    elseif value == 3 then
    text = "将包裹发送到:3市场街,Opix"
    elseif value == 4 then
    text = "将包裹发送到:4市场街,Opix"
    elseif value == 5 then
    text = "将包裹发送到:5市场街,Opix"
    elseif value == 6 then
    text = "将包裹发送到:6市场街,Opix"
    end
    print ("目的地设置完成。")
    script.BoxUi.Value.Value = value
    local gui = script.BoxUi:Clone()
    gui.Parent = plr.PlayerGui
    local box = script.Parent
    box.Parent = plr.Backpack



end)
点赞
用户2858170
用户2858170
本地变量 `text` 等于 `script.BoxUi.TextLabel.Text` 的一个拷贝,而不是对它的引用!引用只能被用于作为对象的表、函数、线程和(完整的)用户数据值。

修改 `text`,例如将其变为

text = "Deliver the Package to: Delivery Depot, Opix"


不会影响 `script.BoxUi.TextLabel.Text`,因为你只是修改了拷贝的值,而不是实际的引用。

你实际想要做的是:

script.BoxUi.TextLabel.Text = "Deliver the Package to: Delivery Depot, Opix"


或者

local text = "Deliver the Package to: Delivery Depot, Opix" script.BoxUi.TextLabel.Text = text


并且代替这样:

local value = math.random(1,6) local text if value == 0 then text = "Deliver the Package to: Delivery Depot, Opix" elseif value == 1 then text = "Deliver the Package to: 1 Market Street, Opix" elseif value == 2 then text = "Deliver the Package to: 2 Market Street, Opix" elseif value == 3 then text = "Deliver the Package to: 3 Market Street, Opix" elseif value == 4 then text = "Deliver the Package to: 4 Market Street, Opix" elseif value == 5 then text = "Deliver the Package to: 5 Market Street, Opix" elseif value == 6 then text = "Deliver the Package to: 6 Market Street, Opix" end


你可以直接写成:

local text = "Deliver the Package to: " .. value .. " Market Street, Opix"


或者

local text = string.format("Deliver the Package to: %d Market Street, Opix", value)

```

不需要那么多 if/elsif 语句

同时注意,math.random(1,6) 不会生成 0,因此你的 0-case 是不需要的。

2020-06-26 08:34:54