Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to make player damage every time he is inside of part?

I used this script to make the player damage.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid:TakeDamage(5)
		wait(1)
	end
end)

But I don't know how to make the player take damage every 1 second he is inside of the part.

3 comments

0

use a while loop with a if inside

0

wait no if

0

a while with the touched event

2 answers

✓ Accepted answer

Make a script inside of the part:

local damage_delay = 1
local damage = 5

local part = script.Parent
local region = Region3.new(part.Position - (0.5 * part.Size), part.Position + (0.5 * part.Size))


while wait(damage_delay)
do
	for _, part in pairs(workspace:FindPartsInRegion3(region, nil, math.huge))
	do
		if part.Name == "HumanoidRootPart" then
		local human = part.Parent:FindFirstChild"Humanoid"
		if human ~= nil then
			human.Health = human.Health - damage
			end
		end
	end
end

What this does is creates a region that has the same size as the part and every time there is part inside of it, it checks if part is called humanoid root part and then it damages the player, damage delay is how fast it damages and damage is damage. Hope this helps.

Also you CAN use while loop but i do not recommend it as it may create a mess and damage you very very fast because you can touch the part again while inside of it and that will start the loop again.

so what you would have to do is use a while loop like this

script.Parent.Touched:Connect(function(hit)
 	while true do
		hit.Parent.Humanoid:TakeDamage(5)
		wait(1)
	end
end)

1 comment

0

Use a while loop in the middle of a touched event isn't needed.

This discussion is preserved as it was. New answers, comments, and votes are closed.