How to damage player if he is outside of a part

So I want to make a kind of battle royale game. I already made a vertical cylinder that becomes smaller and smaller during the game. My problem is: How would I damage a player if he is outside of the cyilinder? (like every 0.5 seconds to damage you 20 or something). I tried looking on the devforum and I realised I can use Region3. But I dont really understand it, and I also dont think it can work on cylinders.

Try using the ZonePlus module since I believe it matches what you need. The documentation and the simple examples ForeverHD gives in the post should be enough to tell you how

Detect when player leaves zone and start damaging them until they enter back

I want something simple not a system

Put a script inside your part and paste this inside. This should work immediately, you can tweak the values as you please.

--//Services
local Players = game:GetService("Players")

--//Variables
local Part = script.Parent

--//Tables
local PlayersInside = {}

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if not Player or table.find(PlayersInside, Player) then
		return
	end
	
	table.insert(PlayersInside, Player)
end)

Part.TouchEnded:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)

	if not Player then
		return
	end
	
	local playerIndex = table.find(PlayersInside, Player)
	
	if playerIndex then
		table.remove(PlayersInside, playerIndex)
	end
end)

while task.wait(1) do
	for i, player in ipairs(Players:GetPlayers()) do
		if not player.Character or table.find(PlayersInside, player) then
			continue
		end
		
		player.Character.Humanoid.Health -= 20
	end
end

I just made what @EmbatTheHybrid explained but without ZonePlus.

1 Like

Well, I can help.

the workspace has several functions you can use.

First, what you want is all the players in the server.
local players = game.Players:GetPlayers()

Then, we need to find the players in the part. Using a workspace function, called workspace:GetPartsInPart, we can find the players characters HumanoidRootPart within the part you want.
We can use a for loop to go through the results of workspace:GetPartsInPart(workspace.YourPart), and find any instances with the name of “HumanoidRootPart.” We’ll test the PlayersService:GetPlayerFromCharacter(CharacterModel), by using the Parent of the HumanoidRootPart, and using an if statement to confirm whether there is a player. That means they’re in the part. Then we can remove them from our newly generated players table. Once we’ve done that, we’ve excluded everyone who’s in our part!

Then you can damage them, by doing a for loop through the table of players, finding the players Humanoid by doing player.Character.Humanoid:TakeDamage(damageAmount).

All done!

Hope this helped, feel free to ask questions.

This is not performant at all and will cause lag if there are many players or if the part is really big. Just use some .Touched events to detect when the player is inside the zone.

Was gonna argue with your solution, but yeah, do agree. They should use .Touched event to index all players.

1 Like