You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua脚本报错:attempt to index nil with 'TookMoney' 求助

问题排查与修复

报错原因

报错"attempt to index nil with 'TookMoney' -script:6"的核心是:Touched事件触发时,Hit.Parent可能不是玩家角色(比如场景中的其他物体),导致players:GetPlayerFromCharacter(Hit.Parent)返回nil,后续代码直接访问nil.TookMoney就会抛出错误。

修复方案

在访问player.TookMoney前先判断player是否有效,同时确保TookMoney属性存在。修改后的代码如下:

local me = script.Parent
local players = game:GetService("Players")

me.Touched:Connect(function(Hit)
    local player = players:GetPlayerFromCharacter(Hit.Parent)
    -- 双重校验:玩家对象存在,且TookMoney属性存在
    if player and player:FindFirstChild("TookMoney") then
        player.TookMoney.Value = false
    end
end)

额外注意事项

  • 确认TookMoney是绑定在玩家(Player对象)上的BoolValue,若该属性在角色模型内,需调整路径为Hit.Parent:FindFirstChild("TookMoney")。
  • 若要避免重复触发,可添加短暂冷却逻辑,或给触发物体设置触摸过滤。

内容的提问来源于stack exchange,提问作者Yoki Gaming

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:45:40