【问题标题】:Why do I get "Unable to cast value to object" when running this function in a remote event?为什么在远程事件中运行此函数时出现“无法将值转换为对象”?
【发布时间】:2018-11-20 06:48:59
【问题描述】:

当我运行此代码时,在第 49 行 (giveOre:FireClient(oreType.Value)) 出现错误提示“无法将值转换为对象”,即使 oreType 被归类为 StringValue。

    function mineOre(plr, target, objTool)
if not target.ClassName == "Model" then --// try to find if target is valid, else set target as target's parent (confusing prob for someone)
    target = target.Parent
end

local oreFolder = target:FindFirstChild("OreStats")
if oreFolder then
    --// Identify ore key vals
    local oreHP = oreFolder:FindFirstChild("OreHP")
    local oreLVL = oreFolder:FindFirstChild("OreLVL")

    local toolLVL = objTool.stats:FindFirstChild("LVL")
    if toolLVL and oreLVL then
        local _math = toolLVL.Value - oreLVL.Value
        if _math >= 0 then
            local toolDMG = objTool.stats:FindFirstChild("DMG")
            oreHP.Value = oreHP.Value - toolDMG.Value

            --// Check if oreHP is 0 or less
            if oreHP.Value <= 0 then
                local oreType = oreFolder:FindFirstChild("OreType")
                local giveOre = plr.Backpack:FindFirstChild("GiveORE")
                giveOre:FireClient(oreType.Value) --// Give the player the ore.

                pcall(function()
                    delay(0.1, function() target:Destroy() end)
                end)
            end
        end
    end
end
    end

这是它在客户端触发的事件,以备不时之需

    function findSec()
local secs = {}

local toSearch = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("Backpack").Area.Inv:GetChildren()
for i,v in pairs(toSearch) do
    local ocu = v:FindFirstChild("Occupied")
    local lock = v:FindFirstChild("Locked")

    if ocu.Value == false and lock.Value == false then
        table.insert(secs, v)
    end
end
end

function giveOre(oreType)
local sec = findSec()
local toSet = sec[1]

toSet.Occupied = true
toSet.Locked = true
toSet.Ore = oreType
end

rem.OnClientEvent:connect(function(oreType)
giveOre(oreType)
end)

谢谢。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    请记住,RemoteEvent:FireClient() 要求第一个参数是您将其发送到的客户端。您收到错误是因为您尝试在程序需要播放器实例的地方发送一个字符串。 正确的代码是

    giveOre:FireClient(plr, oreType.Value)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2019-08-20
      • 2019-09-13
      相关资源
      最近更新 更多