How do I fix/create a damage zone script?

  1. What do you want to achieve?
    I want to make a part that damages you every 1 ½ seconds as long as you are inside the part.

  2. What is the issue?
    I am completely incompetent in terms of scripting, so I have no clue where to begin.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I found a script here on the devforum and tried it, but it doesn’t work:

--Variables--
local Brick = script.Parent
local playerstable = {}
local size = script.Parent.Size.X/2
local cooldown = 1.5 --how long before it attacks again
local damage = 2 --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
			playerstable.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

No clue how to fix it. I also tried looking on the toolbox, but came up empty (probably my wording.)
Does anyone know how to fix the script above? Thanks for any help I can get.

Not asking for someone to make a script for me because I don’t want to break the rules.

Not out of the question though… I think? idk

1 Like

This topic can help you.

It also described there how to make a ‘zone’.
Hope it helps!

2 Likes

It should run inside a server script:

local DamagePart = workspace.SpawnLocation --part path

function getHumanoidsTouching()
	local parts = workspace:GetPartsInPart(DamagePart) 
	local found = {} 
	for i, part in pairs(parts) do 
		local char = part.Parent 
		local hum = char:FindFirstChild("Humanoid") 
		if hum and not table.find(found, hum) then 
			table.insert(found, hum)
		end
	end
	return found 
end

while task.wait(1.5) do 
	local humanoids = getHumanoidsTouching() 
	for i, humanoid in pairs(humanoids) do 
		humanoid:TakeDamage(5)
	end
end
2 Likes
local plrs = game:GetService("Players")
local part = script.Parent
local playerstable = {}
local size = part.Size.X/2
local cooldown = 1.5
local damage = 2

part.Touched:Connect(function(hit)
	if plrs:GetPlayerFromCharacter(hit.Parent) then
		if not table.find(playerstable, hit.Parent.Name) then
			table.insert(playerstable, hit.Parent.Name)
		end
	end
end)

while task.wait(cooldown) do
	for _, playername in ipairs(playerstable) do
		if plrs:FindFirstChild(playername) then
			local plr = plrs:FindFirstChild(playername)
			local char = plr.Character
			local hrp = char:WaitForChild("HumanoidRootPart")
			local hum = char:WaitForChild("Humanoid")
			if (hrp.Position - part.Position).Magnitude > size then
				if table.find(playerstable, playername) then
					table.remove(playerstable, table.find(playerstable, playername))
				end
			else
				if table.find(playerstable, playername) then
					hum:TakeDamage(damage)
				else
					table.insert(playerstable, playername)
					hum:TakeDamage(damage)
				end
			end
		end
	end
end

I know this has been marked as solved but this may assist you. I basically stayed with the logic you originally employed.

1 Like