【问题标题】:script not updating foodBar UI - roblox studio脚本未更新 foodBar UI - roblox studio
【发布时间】:2021-12-06 19:39:45
【问题描述】:

所以对于初学者来说,我有

playerStats(模块脚本)game.StarterPlayer.StarterCharacterScripts

local module = {}

module.hunger = 0
module.thirst = 100

return module

为了不让你开始失去健康饥饿或口渴必须高于 0。现在我将它设为零,这样我就可以测试我制作的食物,它假设会增加 20 饥饿。这确实有效并阻止了健康损失,但它不会更新 foodBar UI。 (我还没有任何渴求的东西)。

我用于更新屏幕和食物的脚本是:

食品(服务器端)工作区.Food.ClickDetector

local food = script.Parent.Parent
local PlayerStats = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
local gain = 2
local playerFunctions = require(game.StarterPlayer.StarterCharacterScripts.playerFunctions)

script.Parent.MouseClick:Connect(function() 
    food:remove()
    PlayerStats.hunger+=gain
    print("Food on click : " .. PlayerStats.hunger)
    playerFunctions.updateFood()
end)

更新 foodBar(模块脚本)game.StarterPlayer.StarterCharacterScripts

local module = {}

module.updateFood = function() 
    local gui = game.StarterGui.ScreenGui
    local PlayerStats = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
    gui.foodBar.Text = "Hunger: " .. PlayerStats.hunger
end

return module

我不认为它是更新食物栏的功能,因为它在我用于检查玩家统计信息的其他脚本中一开始就起作用

local data = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
local player = script.Parent
local humanoid = player:WaitForChild("Humanoid");
local playerFunctions = require(script.Parent.playerFunctions)

task.spawn(function()
    while data.hunger <= 0 or data.thirst <= 0 do
        wait(0.1)
        humanoid.Health-=1
    end
end)

humanoid.Died:Connect(function()
    data.hunger = 100
    data.thirst = 100
end)

while true do
    wait(0.1)
    playerFunctions.updateFood()
end

是的,我知道我在这里放了很多代码,但我对 Lua 很陌生,不知道这可能是什么,所以我想确保你能看到所有内容,以防万一发生意外。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    饥饿条没有更新的原因是因为您正在更新StarterGui(发出Gui的实例)而不是PlayerGui(PlayerGui在Player的实例中)
    另外,不要更新StarterPlayer 中的playerStats 模块,它会将这些统计信息提供给所有玩家。相反,更新 PlayerScripts 内部的模块(它也位于 Player 的实例内部)并需要该模块。

    【讨论】:

    • 我不明白你在说什么。我什至如何从服务器端脚本访问播放器实例。另外,我应该如何更新PlayerScripts 中的playerStats 模块?
    • @SolarFlare 您可以通过不断地访问父级直到到达播放器实例来访问它。例如:您的脚本在 Player.PlayerGui.Gui.Script 中,您必须执行 script.Parent.Parent.Parent 才能访问播放器实例。如果事物没有作为后代的 Player 实例,而是角色,则可以使用 Players.GetPlayerFromCharacter
    • 我还是不明白我想做什么。我什至如何在 roblox 工作室的 Player.PlayerGui.Gui.Script 中获取脚本?我不认为Player.PlayerGui.Gui.Script 事件存在于workspace 或game
    • @SolarFlare 所有播放器实例都在 Players 服务中,要在 PlayerGui 中获取脚本/gui,请将其放在 StarterGui 服务中
    猜你喜欢
    • 2021-12-03
    • 2023-01-26
    • 2023-01-26
    • 2020-07-02
    • 2016-03-31
    • 2019-10-15
    • 2018-09-04
    • 2020-06-23
    • 2018-11-26
    相关资源
    最近更新 更多