如何解决“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)

原文链接 https://stackoverflow.com/questions/69667581

点赞
stackoverflow用户2860267
stackoverflow用户2860267

错误告诉你在第11行, remote 是空的,这说明第2行无法在UserInputService中找到RemoteEvent。根据问题的评论,RemoteEvent不在UserInputService中,实际上在ReplicatedStorage中。

为了解决这个问题,只需更新你的代码,使其指向ReplicatedStorage。

local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("Earthquake")
2021-10-21 21:21:24