Roblox远程事件报错:Character不是Model "Workspace.TRAINING.Nerd"的有效成员
解决Remote Event传递NPC时的"Character不是有效成员"报错
问题根源
客户端的RemoteEvent回调参数定义错误:
服务器通过remote:FireClient(plr, NPC)只传递了一个额外参数(NPC对象),但客户端的回调函数却写了function(plr, npc),这会把服务器传来的NPC当成玩家对象plr。后续调用FightModule.TrainFight(plr, npc)时,实际是把NPC模型传给了需要玩家对象的参数,而NPC模型本身没有Character属性,因此触发报错。
解决方案
修正客户端的回调参数,同时在客户端直接获取本地玩家:
修正后的客户端代码
local FightModule = require(game:GetService("ReplicatedStorage").FightModule) local remote = game:GetService("ReplicatedStorage").Train local localPlr = game.Players.LocalPlayer -- 直接获取本地玩家 remote.OnClientEvent:Connect(function(npc) FightModule.TrainFight(localPlr, npc) end)
关键说明
- Roblox客户端的
RemoteEvent.OnClientEvent回调不会自动传入玩家对象,只有服务器端的OnServerEvent才会将触发事件的玩家作为第一个参数。 - 客户端获取当前玩家的正确方式是
game.Players.LocalPlayer,无需通过RemoteEvent传递。
内容的提问来源于stack exchange,提问作者warre de leeuw
相关产品推荐
相关产品推荐

