如何修复 "ServerScriptService.Earthquake:11: attempt to index nil with 'OnServerEvent'"

How To fix "ServerScriptService.Earthquake:11: attempt to index nil with 'OnServerEvent'"

我已经多次查看代码,但找不到任何可能导致此问题的原因。我是不是瞎了,如果是的话,请指点我正确的方向。

这是我的代码:

local rp = game:GetService("UserInputService")
local remote = rp:WaitForChild("Earthquake")

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local Meshes = script:WaitForChild("Meshes")

local damage = 15

remote.OnServerEvent:Connect(function(player)
    local char = player.Character
    local humrp = char:WaitForChild("HumanoidRootPart")
    local hum = char:WaitForChild("Humanoid")

    local folder = Instance.new("Folder", workspace)
    folder.Name = player.Name.."'s Earthquake"

    local anim = hum:LoadAnimation(script:WaitForChild("anim"))
    anim:Play()

    wait(0.9)

    for count = 1, 15 do 
        local rock = Meshes:WaitForChild("rock"):Clone()
        rock.Parent = folder
        rock.Size = Vector3.new(math.random(4,9),0.05,math.random(4,9))
        rock.Cframe = humrp.CFrame * CFrame.new(0,3,-count*6)
        rock.Orientation = rock.Orientation + Vector3.new(math.random(-20,20),math.random(-20,20),math.random(-20,20))



        local tweenRand = math.random(7,15)




        local tween =  TweenService:Create(rock, TweenInfo.new(0.2),{Size = rock.Size + Vector3.new(0, tweenRand,0), Position = rock.Position + Vector3.new(0,(tweenRand/2)-3,0)})
        tween:Play()

        wait(0.1)
    end
end)

错误告诉你在第 11 行,remote 为 nil,这意味着第 2 行未能在 UserInputService 中找到 RemoteEvent。根据对问题的评论,RemoteEvent 不在 UserInputService 中,它实际上在 ReplicatedStorage 中。

要解决此问题,只需更新您的代码,使其指向 ReplicatedStorage。

local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("Earthquake")