脚本无法更新食物条UI - Roblox Studio
2021-10-19 22:27:3
收藏:0
阅读:131
评论:1
首先,我有
playerStats(模块脚本)game.StarterPlayer.StarterCharacterScripts
local module = {}
module.hunger = 0
module.thirst = 100
return module
为了防止您开始失去健康,饥饿值或口渴值必须高于0。现在我将其设置为零,以便我可以测试我制作的食品项目,该项目应该增加20个饥饿值。它确实有效,并停止了健康损失,但它不会更新foodBar UI。(我暂时还没有关于口渴的任何东西)。
我为更新屏幕和食品项目编写的脚本是:
食品项目(服务器端)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)
更新食品条(模块脚本)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
我不认为是更新foodBar的函数的问题,因为它在我的其他脚本中用于检查玩家状态时可以正常工作。
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非常陌生,不知道可能是什么,所以我希望您能看到所有东西,以防出现意外情况。
原文链接 https://stackoverflow.com/questions/69638270
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
原文如下:
The reason why the hunger bar does not update is because you're updating
StarterGui
(the instance that gives out the Gui) instead ofPlayerGui
(PlayerGui is inside the Player's instance)Also, don't update the
playerStats
module insideStarterPlayer
, it will give those stats to all players. Instead, update the module inside ofPlayerScripts
(which is also located inside of the Player's instance) and require that module instead.翻译结果如下:
饥饿值条不更新的原因是因为你更新了
StarterGui
(提供 GUI 的实例)而不是PlayerGui
(PlayerGui 在玩家实例内部)。另外,不要更新
StarterPlayer
内的playerStats
模块,它会把这些状态授予所有玩家。相反,更新PlayerScripts
内的模块(它也位于玩家实例内部)并要求该模块。