Roblox触摸脚本无法标记玩家问题求助及检测脚本故障排查
Roblox触摸检测脚本问题排查与解决方案
问题根源分析
你的两个脚本失效的核心原因都是混淆了角色模型(Character)和玩家对象(Player):
- 第一个标记脚本中,
humanoid.Parent返回的是玩家的角色模型(Model实例),不是Player对象,所以player:IsA("Player")的判断永远不成立,导致属性设置逻辑完全没执行。 - 第二个检测脚本中,
Touched事件的参数是触碰的部件(Part),不是Player对象,直接调用player:GetAttribute()会报错,因为Part没有这个方法。
修正后的标记脚本
将角色模型转换为对应的Player对象,使用game.Players:GetPlayerFromCharacter()方法:
local playersTouching = {} local targetPart = script.Parent local function onPartTouched(player) if player and player:IsA("Player") then player:SetAttribute("Hiding", true) playersTouching[player] = true end end local function onPartTouchEnded(player) if player and player:IsA("Player") then player:SetAttribute("Hiding", nil) playersTouching[player] = nil end end targetPart.Touched:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") if humanoid then local player = game.Players:GetPlayerFromCharacter(humanoid.Parent) onPartTouched(player) end end) targetPart.TouchEnded:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") if humanoid then local player = game.Players:GetPlayerFromCharacter(humanoid.Parent) onPartTouchEnded(player) end end)
修正后的检测脚本
先从触碰的部件找到角色,再获取Player对象,再判断属性:
local targetPart = script.Parent targetPart.Touched:Connect(function(otherPart) print("TOUCHED") local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") if humanoid then local player = game.Players:GetPlayerFromCharacter(humanoid.Parent) if player and player:GetAttribute("Hiding") then print("标记的玩家触发了触碰") else print("未标记的玩家触发了触碰") end else print("触碰的不是角色部件") end end)
检测玩家同时触摸两个部件的替代方案
如果你的核心需求是判断玩家是否同时触摸script.Parent和另一个目标部件,可以维护一个记录玩家触摸状态的表,跟踪每个玩家当前触摸的部件集合:
local Players = game:GetService("Players") local partA = script.Parent -- 第一个目标部件 local partB = workspace.OtherPart -- 第二个目标部件,替换成你的部件路径 -- 存储每个玩家触摸的部件集合 local playerTouchingParts = {} -- 更新玩家触摸状态的通用函数 local function updatePlayerTouchState(player, part, isTouching) if not player then return end -- 初始化玩家的触摸集合 if not playerTouchingParts[player] then playerTouchingParts[player] = {} end playerTouchingParts[player][part] = isTouching -- 检查是否同时触摸两个部件 local isTouchingBoth = playerTouchingParts[player][partA] and playerTouchingParts[player][partB] player:SetAttribute("IsTouchingBoth", isTouchingBoth) if isTouchingBoth then print(player.Name .. " 同时触摸了两个部件") end end -- 绑定PartA的触摸事件 partA.Touched:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") local player = Players:GetPlayerFromCharacter(humanoid and humanoid.Parent) updatePlayerTouchState(player, partA, true) end) partA.TouchEnded:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") local player = Players:GetPlayerFromCharacter(humanoid and humanoid.Parent) updatePlayerTouchState(player, partA, false) end) -- 绑定PartB的触摸事件 partB.Touched:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") local player = Players:GetPlayerFromCharacter(humanoid and humanoid.Parent) updatePlayerTouchState(player, partB, true) end) partB.TouchEnded:Connect(function(otherPart) local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid") local player = Players:GetPlayerFromCharacter(humanoid and humanoid.Parent) updatePlayerTouchState(player, partB, false) end) -- 玩家离开时清理数据 Players.PlayerRemoving:Connect(function(player) playerTouchingParts[player] = nil end)
内容的提问来源于stack exchange,提问作者Emily Stafford
相关产品推荐
相关产品推荐

