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

Roblox脚本求助:爆炸玩家并发送提示功能报错需修复

问题修复方案

错误原因梳理

  1. 服务端nil引用错误:原代码未检查玩家角色是否存在,当角色未加载/已销毁时,访问HumanoidRootPart会触发nil报错,导致后续逻辑中断。
  2. 客户端脚本类型错误:StarterGui:SetCore仅支持在LocalScript中调用,普通Server Script执行会触发权限报错。
  3. 事件绑定语法错误:原客户端代码错误地调用了函数(humiliation())而非传递函数引用,导致事件绑定失效且参数为nil。
  4. 无效的CoreGui参数:使用了Roblox官方不支持的"test"、"Humiliation"参数,无法正常显示提示。

修复后的服务端代码

local Players = game:GetService("Players")
local ProximityPromptService = game:GetService("ProximityPromptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local function onPromptTriggered(prompt, triggeringPlayer)
    -- 遍历所有在线玩家
    for _, player in ipairs(Players:GetPlayers()) do
        local character = player.Character
        -- 确保角色和关键部件存在
        if character and character:FindFirstChild("HumanoidRootPart") then
            -- 创建爆炸效果
            local explosion = Instance.new("Explosion")
            explosion.BlastRadius = 0.9
            explosion.BlastPressure = 1000000
            explosion.Position = character.HumanoidRootPart.Position
            explosion.Parent = Workspace
            
            -- 向玩家发送提示事件
            ReplicatedStorage.Exploded:FireClient(player, player.Name, triggeringPlayer.Name)
        end
    end
end

ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)

修复后的客户端代码(必须是LocalScript,放在PlayerGui或StarterPlayerScripts中)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")

local function showHumiliationMessage(me, triggerer)
    -- 防御性检查,避免nil参数
    if not me or not triggerer then return end
    
    local notification = {
        Title = "Exploded!",
        Duration = 10
    }

    if triggerer ~= me then
        notification.Text = string.format("You have been exploded by %s.", triggerer)
    else
        notification.Text = string.format("You, %s, exploded yourself.", me)
    end

    -- 使用官方合法的SetCore参数显示提示
    StarterGui:SetCore("SendNotification", notification)
end

-- 正确绑定事件,传递函数引用而非调用结果
ReplicatedStorage.Exploded.OnClientEvent:Connect(showHumiliationMessage)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:46:20