"Unable to cast value to Object"错误信息

所以我使用了一个远程函数,就像下面看到的那样,但出现了一个问题,一个普通的变量赋值不起作用,它给我报错信息"Unable to cast value to Object",是什么问题?

local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function (hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        storeEvent:InvokeClient(slotNum)
    end
end)

连接到其他脚本:

script.Parent.OnInvoke:Connect(function (slot)
    local StoreArrows = game.ReplicatedStorage.StoreArrows
    StoreArrows.SlotNum.Value = slot
    local cam = game.Workspace.Camera
    local storeButtons = script.Parent
    local camNum = game.ReplicatedStorage.StoreArrows.CamNum.Value
    local camNumInst = game.Workspace.CamStorage:WaitForChild("Cam-"..camNum)
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CFrame = camNumInst.CFrame
    local clonedStoreButtons = StoreArrows:Clone()
    clonedStoreButtons.Parent = player.PlayerGui.ScreenGui
end)
点赞
用户2860267
用户2860267

请注意许多客户端同时连接到服务器。所以当你调用RemoteEvent的InvokeClient 函数时,你必须告诉它在哪个客户端上执行。 InvokeClient 的第一个参数应该是玩家,这就是为什么错误告诉你它无法将 slotNum 值转换为玩家对象的原因。

local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function(hit)
    -- 检查触碰的物体是否真的是一个玩家
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        -- 告诉该玩家打开商店
        storeEvent:InvokeClient(player, slotNum)
    end
end)
2020-11-14 00:26:19