Roblox Studio 中的文本值没有变化,Lua

The Text Value isn't Changing in Roblox Studio, Lua

我正在为我的新游戏 Opix Islands 使用我的交付系统,我相信是这个脚本导致了问题。我认为价值正在正确变化,但我不是 100% 确定。我认为问题出在文本的更改上。我已经确保在 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 = "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
    print ("Destination Setting Complete.")
    script.BoxUi.Value.Value = value    
    local gui = script.BoxUi:Clone()
    gui.Parent = plr.PlayerGui
    local box = script.Parent
    box.Parent = plr.Backpack

    



end)
 local text = script.BoxUi.TextLabel.Text

创建 script.BoxUi.TextLabel.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。