How do I make a script that damages a player with a cooldown every few seconds?

I’ve been trying to make a script that consistently damages the player every few seconds if they’re touching a part, but I just can’t seem to figure it out. I’ve tried looking coding it myself, I’ve tried looking on the DevForum, I’ve tried asking ChatGPT, nothing. Extra information: The part is unanchored, a MeshPart of a Head and is Size 4, 4, 4.

1 Like

Have a dictionary where the key is the player and the value is a table which holds baseParts.
(Player : {BasePart})

Whenever the touch event is fired find the player in the table and add the hit part if it isn’t in the table already.

Whenever the touchEnded event is fired remove the hit part from the player’s table.

Lastly loop thru the dictionary, damaging each player’s character if the table contains any part (use # to check).

Alternatively you can just use a zone module to do all this for you.

.Touched and .TouchEnded are quite buggy, they fire while you are still touching the part at times. I’d like to avoid using them for this reason, which is why this has made this so hard.

You could try running a while wait() loop that checks Workspace:FindPartsInRegion3 around the part and damages any players inside, this way is slightly more reliable than .Touched or .TouchEnded imo.

For a system like this, i’d argue that is probably better to use a module like ZonePlus. You can find this here ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries

The most straightforward one I could think of is this:

local part = script.Parent

local damage = 20
local interval = 3 -- the check interval

local filter = {} -- prevents multiple body parts from multiplying the damage

while part and part.Parent do
	task.wait(interval)
	
	local parts = workspace:GetPartBoundsInBox(part.CFrame, part.Size + vector.create(.06, .06, .06))
	
	for _, part in pairs(parts) do
		local character = part.Parent
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		
		if humanoid and not filter[character.Name] then
			filter[character.Name] = true
			
			humanoid:TakeDamage(damage)
		end
	end
	
	filter = {}
end

Although the performance cost could be large.

This is not working for my part. Incase it helps, I have attached the MeshPart. (I have temporarily anchored it so it does not fall when you insert it.)
headmeshpart.rbxm (13.2 KB)

I can’t really think of anything. But maybe this works?

local part = script.Parent

local damage = 20
local interval = 2 -- the check interval

local filter = {} -- prevents multiple body parts from multiplying the damage

while part and part.Parent do
	task.wait(interval)

	local parts = workspace:GetPartBoundsInRadius(part.Position, part.Size.Y + .08)

	for _, part in pairs(parts) do
		local character = part.Parent
		local humanoid = character:FindFirstChildOfClass("Humanoid")

		if humanoid and not filter[character.Name] then
			filter[character.Name] = true

			humanoid:TakeDamage(damage)
		end
	end

	filter = {}
end

I made the interval faster, and used GetPartBoundsInRadius instead of GetPartBoundsInBox now.

This is still not working, I am jumping on the part and circling around it and I even set the interval very low and yet nothing happened still.

Probably because I didn’t add a .Touched event, it checks every interval seconds without taking for account if something touches it.

This did not work. I tried both:

local zoneplus = require(game.ServerScriptService.Zone)
local zone = zoneplus.new(script.Parent)

zone.playerEntered:Connect(function(plr)
	plr.Humanoid:TakeDamage(5)
end)

and

local zoneplus = require(game.ServerScriptService.Zone)
local zone = zoneplus.fromRegion(script.Parent.CFrame, Vector3.new(4.125, 4.125, 4.125))

zone.playerEntered:Connect(function(plr)
	print(plr)
	plr.Humanoid:TakeDamage(5)
end)

This didn’t work at first, but I realised I had been dragging the part on the client causing it to not actually be in the location I thought it was, causing the script to not work properly. After fixing this, the script did actually start to work. Here is the final script I came up with (If there any improvements that could be made, please tell me, I would like to know.):

while task.wait() do
	for _, plr in pairs(workspace:FindPartsInRegion3(Region3.new(script.Parent.Position - (0.5 * script.Parent.Size), script.Parent.Position + (0.5 * script.Parent.Size)), script.Parent, 100)) do
		if game.Players:GetPlayerFromCharacter(plr.Parent) then
			if script:FindFirstChild(plr.Parent.Name) then
				if script:FindFirstChild(plr.Parent.Name).Value + 3 < workspace.DistributedGameTime then
					script:FindFirstChild(plr.Parent.Name):Destroy()
					plr.Parent.Humanoid:TakeDamage(5)
				end
			else
				local cooldown = Instance.new("NumberValue", script)
				cooldown.Name = plr.Parent.Name
				cooldown.Value = workspace.DistributedGameTime
				plr.Parent.Humanoid:TakeDamage(5)
			end
		end
	end
end

If you’re looking for improvements, here are a couple I implemented below:

  • Change FindPartsInRegion3 and repeated Region3.new calls with GetPartsInPart.
  • Replace while task.wait() do with RunService.Heartbeat
  • Local debounce cache instead of using Instances
local timeBetweenHits = 3
local deb = {} :: { [Humanoid]: any }
local delay = task.delay
local part = script.Parent
local RunService = game:GetService("RunService")
local OP = OverlapParams.new()
OP.RespectCanCollide = false

local function remove(hum)
	deb[hum] = nil
end

RunService.Heartbeat:Connect(function()
	local parts = workspace:GetPartsInPart(part, OP)
	for _, part in parts do
		local hum = part.Parent:FindFirstChildOfClass("Humanoid")
		if hum and not deb[hum] then
			deb[hum] = true
			hum:TakeDamage(5)
			delay(timeBetweenHits, remove, hum) -- avoid making a bunch of closures like this
		end
	end
end)

Apologies if there are any errors, I did this on mobile lol
EDIT: formatting & fix

EDIT 2: You might have to make this use part:GetTouchingParts() since you want it to be when you touch the part. However I think this using a hitbox part may be better.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.