Kill players outside of an area

Hey guys! So I was trying to figure out how to make a script, what I want to do is kill any player that is outside of a room, I already have the region set and I can figure out which players are inside the region. But how can I find which players are not in the region and make them die? Thanks!

Maybe make 2 seperate tables containing players in region and players not in region. And then maybe for loop in players not in region, and check if these non players are in the region or not, if not, start decreasing their health.

1 Like

You’re going to want to get all characters in the area, you can do this by detecting what parts are in the Region and then seeing if they’re a players character, then iterate through players and if a player isn’t in the region because you didn’t detect their character, kill them.

1 Like

Basically loop through all the parts in the region, and check if their parent is the character. If it is, then insert them into the “SafeTable”. Then do this loop:

local SafeTable = {} --contains names of safe players

for i, player in pairs(game.PLayers:GetChildren()) do
       if not SafeTable[player.Name] then --check if they are not in the table.
            player.Character.Humanoid.Health = 0
       end
end
2 Likes

I was busy so I just started testing, sorry. But, when I loop through the region and print it, it comes up the first body part of my character to hit the region so when I use that loop I just die. I’m new to scripting so I’m kind of stuck, how can I get the player instead of the character parts?

Can I see your current code, I can help you modify it to make it work.

I actually did figure it out, I had someone in a discord server help me out!! Also I gave you the solution because you helped point me in the right direction, thanks!
If anyone is wondering or has this issue heres the working code

local RegionPart = game.Workspace.TestRegion
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local region = Region3.new(pos1, pos2)

local SafeTable = {}



while true do
	wait(10)
	local PlayersInRegion = workspace:FindPartsInRegion3(region, RegionPart, 1000)
	for i, part in pairs(PlayersInRegion) do
		if part.Parent and part.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(part.Parent)
			if player then
				SafeTable[player.Name] = true
				print(SafeTable)
			end
		end
	end
	for i, player in pairs(game.Players:GetChildren()) do
		if not SafeTable[player.Name] then
			player.Character.Humanoid.Health = 0
		end
	end
end
3 Likes