Help with knowing if player is in block

hello, i have a AI system and some saferooms, i wanted to make it where if u hide in dark areas which there are saferooms in, the ai cant get u, which works already, but i wanted to make it where if the player equips his light, the saferoom (Hes currently at, not the other saferooms) go untouchable, which makes it where the saferoom doesnt work, i need a way to send a remote event that constantly checks if hes in a saferoom with his light equipped and if so, the one hes at goes untouchable. Currently all i have is a remote event going to a serverscript for when ever tool is .Equipped. ANy help?

You can use region3 or :GetPartsInPart() to detect if the player character’s part is in the block. More detail on the function here WorldRoot | Documentation - Roblox Creator Hub and detail for region3 Region3 | Documentation - Roblox Creator Hub

This script loops through a table of all of the safe zones. It initially assumes they are not safe, but if any body part of the player is in any safe zone then they are considered safe.

local safeZones = game.Workspace.safeZones:GetChildren() -- safe zones here, could also be like {game.Workspace.SafeZone1, game.Workspace.SafeZone2}

while task.wait() do -- do this check every frame
	
	for i,plr in pairs(game.Players:GetChildren()) do -- check every player
		
		local isSafe = false -- assume that they are not safe at first
		
		for i1, safeZone in pairs(safeZones) do -- check through the safe zones
			
			local touching = game.Workspace:GetPartsInPart(safeZone) -- get all parts that are touching the safezone
			for i2, part in pairs(touching) do -- loop through all of those parts
				local char = plr.Character -- get the character
				if not char then continue end -- dont do anything if there is no character

				if part.Parent == char then -- check if one of the parts is a body part of that character
					isSafe = true -- if any body part is touching any safe zone, then the player is safe
					break -- dont need to check any further if they are safe
				end
			end
			if (isSafe == true) then break end -- dont need to check any further if they are safe
		end
		
		-- do something with isSafe
	end
end
1 Like