脚本没有更新foodBar UI - roblox studio



首先是

playerStats (module script) game.StarterPlayer.StarterCharacterScripts

local module = {}
module.hunger = 0
module.thirst = 100
return module

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

我有更新屏幕和食物项目的脚本是:

food item(服务器端)workspace.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)

update foodBar (module script) 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的新手,不知道这可能是什么,所以我想确保你能看到一切,以防万一它是意想不到的。

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

最新更新