Roblox远程函数中TextService名称过滤失效问题求助
Roblox 姓名性别设置GUI脚本故障排查与修复
问题概述
正在开发一套通过GUI设置玩家姓名与性别的Roblox脚本,包含4个关联模块:
- 检测DataStore中是否存储
firstName,未存储则显示设置GUI - 保存玩家姓名与性别的RemoteFunction
- 调用TextService检测名称合规性的RemoteFunction(本地脚本无法访问DataStore/TextService)
- 关联GUI,调用上述RemoteFunction完成数据保存的本地脚本
当前核心问题:第3个名称过滤检测脚本执行到打印Checking Name.后无后续输出,RemoteFunction未正常完成验证,导致点击确认按钮后无法进入保存流程,预期的Safe、Saving...日志未输出,GUI也未销毁。
原脚本代码
1. 显示GUI脚本(ServerScript)
local DataStoreService = game:GetService("DataStoreService") local playerDataStore = DataStoreService:GetDataStore("ChaosDataStore03") local ServerStorage = game:GetService("ServerStorage") local NamingGui = ServerStorage:FindFirstChild("NamingGui") game.Players.PlayerAdded:Connect(function(player) local success, result = pcall(function() return playerDataStore:GetAsync(player.UserId) end) if not success or result.firstName == nil then local guiClone = NamingGui:Clone() guiClone.Parent = player.PlayerGui end end)
2. 保存数据RemoteFunction(ServerScript)
local DataStoreService = game:GetService("DataStoreService") local playerDataStore = DataStoreService:GetDataStore("ChaosDataStore03") game.ReplicatedStorage.SaveNameAndGender.OnServerInvoke = function(player, name, gender) print("Saving...") local success, err = pcall(function() playerDataStore:SetAsync("firstName_"..player.UserId, name) playerDataStore:SetAsync("playerGender_"..player.UserId, gender) end) if success then print("Saved successfully") else print("Save Failed: "..err) end end
3. 名称过滤检测RemoteFunction(ServerScript)
game.ReplicatedStorage.FilterCheck.OnServerInvoke = function(player, text) local TextService = game:GetService("TextService") print("Checking Name.") local filtered = TextService:FilterStringAsync(text, player.UserId) if filtered == text then print("Safe") else print("Filtered") end end
4. GUI本地脚本(LocalScript)
local NamingGui = game.Players.LocalPlayer.PlayerGui.NamingGui local NameBox = NamingGui.NameBox local MaleButton = NamingGui.MaleButton local FemaleButton = NamingGui.FemaleButton local ConfirmButton = NamingGui.ConfirmButton local genderstuff = "" MaleButton.MouseButton1Click:Connect(function() genderstuff = "Male" print("Male Selected.") end) FemaleButton.MouseButton1Click:Connect(function() genderstuff = "Female" print("Female Selected.") end) ConfirmButton.MouseButton1Click:Connect(function() print("Checking...") local name = NameBox.Text if #name <= 15 and genderstuff ~= "" then local success, result = game.ReplicatedStorage.FilterCheck:InvokeServer(name) if success and result == "Safe" then game.ReplicatedStorage.SaveNameAndGender:InvokeServer(name, genderstuff) NamingGui:Destroy() end end end)
故障原因分析
- 过滤检测脚本核心错误:
FilterStringAsync返回的是TextFilterResult对象,并非直接返回过滤后的字符串,直接用filtered == text比较会永远不成立,且脚本未向客户端返回任何结果,导致客户端InvokeServer一直等待,流程卡住。 - DataStore键不匹配:显示GUI脚本中用
player.UserId作为键读取数据,但保存脚本中用firstName_+player.UserId作为键,导致永远读取不到已保存的firstName,GUI会重复显示。 - 错误处理缺失:过滤检测脚本未对
FilterStringAsync进行pcall包裹,若服务调用失败会直接抛出错误,导致RemoteFunction中断。
修复后的完整脚本
1. 修复后的显示GUI脚本
local DataStoreService = game:GetService("DataStoreService") local playerDataStore = DataStoreService:GetDataStore("ChaosDataStore03") local ServerStorage = game:GetService("ServerStorage") local NamingGui = ServerStorage:FindFirstChild("NamingGui") game.Players.PlayerAdded:Connect(function(player) local success, result = pcall(function() -- 匹配保存时的键名 return playerDataStore:GetAsync("firstName_"..player.UserId) end) -- 读取失败或无数据时显示GUI if not success or result == nil then local guiClone = NamingGui:Clone() guiClone.Parent = player.PlayerGui end end)
2. 优化后的保存数据RemoteFunction
local DataStoreService = game:GetService("DataStoreService") local playerDataStore = DataStoreService:GetDataStore("ChaosDataStore03") game.ReplicatedStorage.SaveNameAndGender.OnServerInvoke = function(player, name, gender) print("Saving...") local success, err = pcall(function() -- 用单个键存储玩家数据,减少DataStore调用次数 playerDataStore:SetAsync(player.UserId, { firstName = name, playerGender = gender }) end) if success then print("Saved successfully") return true else print("Save Failed: "..err) return false, err end end
3. 修复后的名称过滤检测RemoteFunction
local TextService = game:GetService("TextService") game.ReplicatedStorage.FilterCheck.OnServerInvoke = function(player, text) print("Checking Name.") local success, filteredResult = pcall(function() return TextService:FilterStringAsync(text, player.UserId) end) if not success then print("Filter check failed: "..filteredResult) return false, "Filter service error" end -- 获取适合公开显示的过滤后文本 local successFilter, filteredText = pcall(function() return filteredResult:GetNonChatStringForBroadcastAsync() end) if not successFilter then print("Failed to get filtered text: "..filteredText) return false, "Failed to retrieve filtered text" end -- 判断是否被过滤 if filteredText == text then print("Safe") return true, "Safe" else print("Filtered") return true, "Filtered" end end
4. 适配后的GUI本地脚本
local NamingGui = game.Players.LocalPlayer.PlayerGui.NamingGui local NameBox = NamingGui.NameBox local MaleButton = NamingGui.MaleButton local FemaleButton = NamingGui.FemaleButton local ConfirmButton = NamingGui.ConfirmButton local genderstuff = "" MaleButton.MouseButton1Click:Connect(function() genderstuff = "Male" print("Male Selected.") end) FemaleButton.MouseButton1Click:Connect(function() genderstuff = "Female" print("Female Selected.") end) ConfirmButton.MouseButton1Click:Connect(function() print("Checking...") local name = NameBox.Text:Trim() -- 去除首尾空格 if #name > 0 and #name <= 15 and genderstuff ~= "" then local success, result = game.ReplicatedStorage.FilterCheck:InvokeServer(name) if success and result == "Safe" then local saveSuccess, saveErr = game.ReplicatedStorage.SaveNameAndGender:InvokeServer(name, genderstuff) if saveSuccess then NamingGui:Destroy() print("Setup completed") else print("Save failed: "..saveErr) end else print("Name invalid: "..(result or "Unknown error")) end else print("Invalid input: Name must be 1-15 characters and gender selected") end end)
内容的提问来源于stack exchange,提问作者Kachi
相关产品推荐
相关产品推荐

