Roblox脚本求助:爆炸玩家并发送提示功能报错需修复
问题修复方案
错误原因梳理
- 服务端nil引用错误:原代码未检查玩家角色是否存在,当角色未加载/已销毁时,访问
HumanoidRootPart会触发nil报错,导致后续逻辑中断。 - 客户端脚本类型错误:
StarterGui:SetCore仅支持在LocalScript中调用,普通Server Script执行会触发权限报错。 - 事件绑定语法错误:原客户端代码错误地调用了函数(
humiliation())而非传递函数引用,导致事件绑定失效且参数为nil。 - 无效的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
相关产品推荐
相关产品推荐

