How to make a damaging zone

How would I make it where whenever a player is in a zone they get damaged every second.

I’ve tried something using this script but it only damages the player when they first touch the part, not if they stay inside of the part.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5
	end
end)

Thanks.

1 Like

Put this inside StarterCharacterScripts:

while wait(1) do
    if table.find(workspace:GetPartsInPart(workspace.DamagingZone), script.Parent.HumanoidRootPart) then
        script.Parent.Humanoid:TakeDamage(5)
    end
end
4 Likes
local Players = game:GetService("Players")

local ZoneStart, ZoneEnd = Vector3.new(-10, -10, -10), Vector3.new(10, 10, 10)

local function Zone()
	while task.wait(1) do
		for Index, Player in pairs(Players:GetPlayers()) do
			if Player.Character then
				local HumanoidRootPart = Player.Character.HumanoidRootPart
				local Humanoid = Player.Character.Humanoid
				
				if HumanoidRootPart and Humanoid then
					if HumanoidRootPart.Position >= ZoneStart and HumanoidRootPart.Position <= ZoneEnd then
						Humanoid:TakeDamage(10)
					end
				end
			end
		end
	end
end

task.spawn(Zone)

1 Like

That didn’t work but thanks for helping

1 Like

What am I supposed to put in these values?

1 Like

Did you replace workspace.DamagingZone with the part with the part that damages the player?
I put it into Roblox Studio myself and it works fine

2 Likes

Really? I did that, let me try again.

1 Like

Nevermind, it works perfectly, after an hour of trying and finding solutions I now just realized that the whole time the part wasn’t anchored.

1 Like

Anchored should honestly not cause a difference?

1 Like

Yes it caused a difference because when the part was not anchored it glitched its way into the ground. The part was also transparent so I had no idea that it was underground and that I was not actually interacting with it.

1 Like

I have just used this script into a union and it lags the game every second