How do I make a zone damage a player when entered?

I want to make a certain zone/part damage the player once entered.

The current script only works if you touch the face of the part. Here:

I tried looking everywhere but couldn’t find the answer and because I cannot formulate the problems sentence.

I have made a simple piece of code but it only damages when you touch the face as said above. This is the block of code:

--Variables--
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = Parent.Humanoid.Health - 10
		wait(10)
	end
end

Brick.Touched:connect(PlayerTouched)

Keep in mind this is a pretty old piece of script, I just want to know whats wrong.
I am really sorry if this Topic has a few mistakes, its my second time writing after all.

Use a Region3 and workspace:FindPartsInRegion3()

2 Likes

I think AlvinBlox did a tutorial on sound regions, but I think you can use the same template but to damage the player.

3 Likes

Just an idea. You insert their name into a table when they touch the part, then every 10 seconds you reduce everyone’s health who is in the table.

--Variables--
local Brick = script.Parent
local playerstable = {}
local size = script.Parent.Size.X/2
local cooldown = 5 --how long before it attacks again
local damage = 10 --damage
--End--

--Code--
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		if not playerstable[Parent.Name] then
		      table.insert(playerstable, Parent.Name)
		end
	end
end

Brick.Touched:connect(PlayerTouched)

while wait(cooldown) do
       for i, v in pairs(playerstable) do
              if game.Players:FindFirstChild(v) and (game.Players:FindFirstChild(v).Character.HumanoidRootPart.Position - Brick.Position).Magnitude >  size-- check if they left the part  then
                     table.remove(playerstable, playerstable[v] --remove them from the table
              end
       end
      wait(0.3)
       for i, v in pairs(playerstable) do
             if  game.Players:FindFirstChild(v) then
                     game.Players:FindFirstChild(v).Character.Humanoid.Health -=damage
              end
       end
end
3 Likes

Never thought of that will try it out tho, it should work.
Also thanks for the reply!

But you see there will be multiple areas and then it would just take up a lot of script although I could check in each area.

1 Like

You could just add a for loop that checks through all of the parts in a predefined folder, that would be much better then just copying and pasting the code and modifying it a small bit.

1 Like

Thats what I meant with checking in each area with one script.