Roblox新手求助:脚本遇两类Value相关报错问题
Roblox脚本报错修复方案
报错1:attempt to index number with 'Value'
问题原因
- 你把
price定义成了普通数字10,数字类型没有Value属性,代码里却用price.Value去访问,触发了索引数字的错误。 - 另外
game.ReplicatedStorage.Milk如果是IntValue或NumberValue这类值对象,直接用milk引用的是对象本身,要获取它的数值必须用milk.Value,否则直接拿对象和数字比较逻辑不成立。
修正后的代码
local milk = game.ReplicatedStorage.Milk -- 确认这是IntValue/NumberValue类型对象 local price = 10 -- 普通数字变量,无需.Value script.Parent.MouseButton1Click:Connect(function() -- 用milk.Value获取实际数值进行判断和修改 if milk.Value >= price then milk.Value = milk.Value - price price = price + 10 -- 直接修改数字变量 else print("牛奶不足,无法购买") end end)
报错2:Value is not a valid member of LocalScript
问题原因
FindFirstChild("shopSystem")找到的是一个LocalScript文件,而不是IntValue/NumberValue这类带Value属性的数值对象,所以访问price.Value会报错。- 另外
while true循环里没有添加wait(),会导致游戏主线程卡死,必须在循环内加入等待逻辑。
修正后的代码
-- 找到正确的数值对象,比如shopSystem下的Price值对象(根据你的实际结构调整) local priceValue = script.Parent.Parent.BuyButton.shopSystem:FindFirstChild("Price") while true do wait(0.1) -- 添加等待,避免卡死游戏 if priceValue then -- 先判断对象是否存在,防止空引用报错 script.Parent.Parent.PriceText.Text = priceValue.Value .. " Milk" else script.Parent.Parent.PriceText.Text = "加载中..." end end
内容的提问来源于stack exchange,提问作者pilot rl
相关产品推荐
相关产品推荐

