【问题标题】:"Unable to cast value to Object" error message“无法将值转换为对象”错误消息
【发布时间】:2020-11-14 08:26:19
【问题描述】:

所以我使用了一个远程函数,如最后所示,由于某种原因,一个变量的正常赋值不起作用,它给我一个错误消息,“无法将值转换为对象”怎么了?

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)

【问题讨论】:

  • 错误指向的行是什么?
  • 发送脚本的第 5 行
  • 我认为这是一个范围问题。将该行放在第二个脚本的开头,并在函数之外定义该变量。 local storeButtons = script.Parent storeButtons.OnInvoke:Connect(function (slot)...

标签: lua roblox


【解决方案1】:

请记住,许多客户端一次连接到服务器。因此,当您调用RemoteEvent's InvokeClient 函数时,您必须告诉它在哪个 客户端上调用它。 InvokeClient 的第一个参数应该是玩家,这就是为什么错误告诉您它不能将 slotNum 值转换为玩家对象。

local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function(hit)
    -- check that the thing that we touched is actually a player
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        -- tell that player to open the store
        storeEvent:InvokeClient(player, slotNum)
    end
end)

【讨论】:

  • 我刚才试过了,现在什么也没做
  • 我看到您将此标记为答案,即使您说它不起作用。你用连接脚本解决了你的问题吗?
  • 是的,它有帮助,我现在找到了问题,远程事件没有存储在复制存储中,它现在可以正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
相关资源
最近更新 更多