为什么我在远程事件中 运行 这个函数时得到 "Unable to cast value to object"?
Why do I get "Unable to cast value to object" when running this function in a remote event?
当我 运行 这段代码时,第 49 行出现错误 "Unable to cast value to object" (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)
谢谢。
请记住,RemoteEvent:FireClient() 要求第一个参数是您要将其发送到的客户端。您收到错误是因为您尝试在程序需要 Player 实例的地方发送一个字符串。
正确的代码是
giveOre:FireClient(plr, oreType.Value)
当我 运行 这段代码时,第 49 行出现错误 "Unable to cast value to object" (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)
谢谢。
请记住,RemoteEvent:FireClient() 要求第一个参数是您要将其发送到的客户端。您收到错误是因为您尝试在程序需要 Player 实例的地方发送一个字符串。 正确的代码是
giveOre:FireClient(plr, oreType.Value)