You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Roblox报错“ServerScriptService.Earthquake:11: attempt to index nil with 'OnServerEvent'”

可直接触发报错的代码问题梳理

  • 服务调用逻辑错误:UserInputService 是客户端专属服务,无法在服务器侧获取,你将名为Earthquake的RemoteEvent存放在该服务下,服务器脚本执行rp:WaitForChild("Earthquake")时会直接超时抛出找不到实例的报错。请将RemoteEvent迁移到ReplicatedStorage服务下,服务器和客户端都可以正常访问该路径下的实例,同时建议修改变量名rp为UIS或UserInputService,避免和常用的HumanoidRootPart缩写rp混淆。
  • 属性拼写错误:代码中rock.Cframe的属性名拼写错误,Roblox中对应属性为大写F的CFrame,给不存在的属性赋值会直接抛出异常。
  • 空索引风险:未对player.Character做判空处理,如果触发远程事件时玩家角色尚未加载/已经被销毁,后续调用char:WaitForChild会直接报错,建议将获取角色的逻辑替换为local char = player.Character or player.CharacterAdded:Wait()
  • 弃用方法兼容风险:Humanoid:LoadAnimation已被官方标记为弃用,建议替换为Humanoid.Animator:LoadAnimation()调用,避免后续版本更新后出现调用失效问题。
  • 资源泄漏风险:已引入Debris服务但未使用,生成的地震文件夹、岩石实例不会自动销毁,长期运行会占用服务器资源,建议在逻辑结束后调用Debris:AddItem(folder, 3)自动清理生成的资源。

修复后参考代码

-- 服务器脚本建议放在ServerScriptService下
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("Earthquake") -- RemoteEvent迁移到ReplicatedStorage下

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 or player.CharacterAdded:Wait()
    local humrp = char:WaitForChild("HumanoidRootPart")
    local hum = char:WaitForChild("Humanoid")
    local animator = hum:WaitForChild("Animator")

    local folder = Instance.new("Folder")
    folder.Name = player.Name.."'s Earthquake"
    folder.Parent = workspace
    -- 自动3秒后销毁所有生成的资源
    Debris:AddItem(folder, 3)

    -- 用Animator加载动画
    local anim = animator:LoadAnimation(script:WaitForChild("anim"))
    anim:Play()

    task.wait(0.9) -- 建议用task.wait替代wait,精度更高

    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) -- 修正CFrame拼写
        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()
        -- 动画播放完自动销毁岩石,可选
        tween.Completed:Once(function()
            Debris:AddItem(rock, 0.1)
        end)

        task.wait(0.1)
    end
end)

内容的提问来源于stack exchange,提问作者The_OneNeo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 07:39:00