参数 1 缺失或为零 - Roblox Lua

Argument 1 missing or nil - Roblox Lua

我正在制造一种您可以开采的矿石,但是当我试图开采它直到它的生命值变为 0 时,我应该得到 1 gem(它给出 1 gem 当我开采它时),但它给出了错误:

14:09:24.711  Argument 1 missing or nil  -  Server - Script:4
14:09:24.711  Stack Begin  -  Studio
14:09:24.712  Script 'Workspace.Gem.ClickDetector.Script', Line 4  -  Studio - Script:4
14:09:24.712  Stack End  -  Studio

脚本 1 (game.Workspace.Gem.ClickDetector.Script):

script.Parent.MouseClick:Connect(function()
    script.Parent.Parent.Health.Value = script.Parent.Parent.Health.Value - 1
    if script.Parent.Parent.Health.Value < 1 then
        workspace.GetOres.Gems:FireClient()
        script.Parent.Parent.Transparency = 1
        script.Parent.Parent.CanCollide = false
        wait(9)
        script.Parent.Parent.Transparency = 0
        script.Parent.Parent.CanCollide = true
        script.Parent.Parent.Health.Value = 10
     end
 end)

脚本 2 (game.Workspace.GetOres):

script.Gems.OnClientEvent:Connect(function()
    local leaderstats = game.Players.LocalPlayer:FindFirstChild("leaderstats")
    leaderstats.Gems.Value = leaderstats.Gems.Value + 1
end)

此外,脚本 2 是 LocalScript。

我假设您是在 LocalScript 而不是主脚本中修改 Gems leaderstat,因为它是一个隐藏值,您只希望本地玩家看到它。

您遇到了三个问题:

首先,当您使用RemoteEvent's FireClient function, you are communicating from the server down to a specific client. Because there can be many clients, you need to specify which client you are firing this message to. The error is telling you that the first argument is missing, that's because it is supposed to be a Player. You can easily get that Player from the ClickDetector's MouseClick事件时。看起来像这样:

script.Parent.MouseClick:Connect(function(playerWhoClicked)
    game.Workspace.GetOres.Gems:FireClient(playerWhoClicked)

接下来,您的 LocalScript won't actually receive the event. LocalScript's are only active in a handful of places, and it cannot be in the Workspace. So I would recommend that you move it to StarterPlayer.StarterPlayerScripts.

第三,由于 RemoteEvent 位于该 LocalScript 中,您还需要将其移动到服务器脚本可访问的某个位置。我建议将其移至 ReplicatedStorage。您只需像这样更新该事件的路径:

local getGemsEvent = game.ReplicatedStorage.Gems
getGemsEvent:FireClient(playerWhoClicked)

总的来说,看起来像这样:

local detector = script.Parent
local gem = script.Parent.Parent
local getGemsEvent = game.ReplicatedStorage.Gems

detector.MouseClick:Connect(function(playerWhoClicked)
    gem.Health.Value = gem.Health.Value - 1
    if gem.Health.Value < 1 then
        getGemsEvent:FireClient(playerWhoClicked)
        gem.Transparency = 1
        gem.CanCollide = false
        wait(9)
        gem.Transparency = 0
        gem.CanCollide = true
        gem.Health.Value = 10
     end
 end)