我的Roblox Studio脚本有时会出现错误..不知道为什么

所以.. 我编程使GUI显示玩家的货币数量,数据存储库API完美运行,但是本地脚本不起作用(如果不是局部的,它将只是每次玩家的货币得到更新时就更新它,而且会是我想要的相反的状态),有时它会将货币加载到GUI中,但有时它仅停留在原始文本上:"Label"而不是我的当前货币(4600)。 下面是证明

通常发生并应始终发生的情况

No script output with currency loaded in GUI

有时会发生的而不应该发生的情况:

Script output with original text "label" instead of current currency "4600" 这是脚本,我试过在开始时等待,但原始代码在while true do..内部

wait(game.Players.LocalPlayer:WaitForChild("Data")
wait(game.Players.LocalPlayer.Data:WaitForChild("Bells"))
while true do
    script.Parent.TextLabel.Text = game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Value
    wait() --wait is for not making the loop break and stop the whole script
end

好吧..如果你想看看数据是否实际上在玩家中,在这里是脚本,它需要API(DataStore2)

--[Animal Crossing Roblox Edition Data Store]--
            --Bryan99354--
        --Module not mine--
--Made with a AlvinBlox tutorial--
--·.·.*[Get Data Store, do not erase]*.·.·--

local DataStore2 = require(1936396537)

--[Default Values]--

local DefaultValue_Bells = 300
local DefaultValue_CustomClothes = 0

--[Data Store Functions]--

game.Players.PlayerAdded:Connect(function(player)
    --[Data stores]--
    local BellsDataStore = DataStore2("Bells",player)

    local Data = Instance.new("Folder",player)
    Data.Name = "Data"

    Bells = Instance.new("IntValue",Data)
    Bells.Name = "Bells"

    local CustomClothesDataStore = DataStore2("CustomClothes",player)

    local CustomClothes = Instance.new("IntValue",Data)
    CustomClothes.Name = "CustomClothes"

    local function CustomClothesUpdate(UpdatedValue)
        CustomClothes.Value = CustomClothesDataStore:Get(UpdatedValue)
    end

    local function BellsUpdate(UpdatedValue)
        Bells.Value = BellsDataStore:Get(UpdatedValue)
    end

    BellsUpdate(DefaultValue_Bells)
    CustomClothesUpdate(DefaultValue_CustomClothes)

    BellsDataStore:OnUpdate(BellsUpdate)
    CustomClothesDataStore:OnUpdate(CustomClothesUpdate)
end)

--[test and reference functions]--

workspace.TestDevPointGiver.ClickDetector.MouseClick:Connect(function(player)
    local BellsDataStore = DataStore2("Bells",player)
    BellsDataStore:Increment(50,DefaultValue_Bells)
end)
workspace.TestDevCustomClothesGiver.ClickDetector.MouseClick:Connect(function(player)
    local CustomClothesDataStore = DataStore2("CustomClothes",player)
    CustomClothesDataStore:Increment(50,DefaultValue_CustomClothes)
end)

创建"Data"和"Bells"的代码位于评论中: 数据存储库(Data Stores) 唯一会出现问题的脚本是短的没有理由:< 希望你能帮助我:3 @Night94我尝试过你的脚本,但有时也会失败 New script with same random script output error

点赞
用户1296374
用户1296374

你的 LocalScript 中等待时间的语法有点问题。修正之后,它每次都能正常工作。此外,我建议使用事件处理方式,而不是使用循环来更新数值:

game.Players.LocalPlayer:WaitForChild("Data"):WaitForChild("Bells").Changed:Connect(function(value)
    script.Parent.TextLabel.Text = value
end)
2020-05-25 16:40:05