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

how do i make code that slowly does damage to a plyr, when they r in it?

Here is a script that slowly does damage while the player is in it. Unfortunately it doesn't work all the time. I put it in a block and put the blocks down different places so when the player walks through it it does damage.


  local damage = 1
	  local touched
	        local timebeforeotherdamage = 1
	 
	 
	 
	script.Parent.Touched:Connect(function(hit)
	    if hit.Parent:FindFirstChild("Humanoid") then
	        if touched == false then
	              touched = true
	        repeat
	     if touched == true then
	        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - damage
	 
	end
	wait(timebeforeotherdamage)
	        until
	        hit.Parent.Humanoid.Health == 0
	        end
	 
	    end
	    repeat
	    wait()
	    until
	    touched == false
	 
	 
	 
	 
	 
	end)
	script.Parent.TouchEnded:Connect(function(hit)
	    touched = false
	end) ~~~~~~~~~~~~~~~~~


sometimes it will do a little damage, but when you go out of the block and back in, it does A LOT. The code looks fine to me so I don't know whats wrong...


5 comments

0

Use FindPartsInRegion3 (https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3) instead, it is way more reliable in this situation.

0

I will just warn you guy I'm still learning, so talk to me like a 5 year old

0

A region3 is made by specifying the top right and bottom left points of the box you want to do damage in. Once you have the region made, you can find parts in it and do damage to the humanoid.

0

I would highly suggest Region3s, to learn how to get players in a Region3, a good example is AlvinBlox's Robbable Bank Tutorial, which includes the use of region3s.

0

ok

1 answer

This might work! Put this in a script inside of your damage part. Change the wait(1) to change how many seconds you want to wait in between damaging. Change the hum:TakeDamage(50) to how much damage you want to do. If this doesn't work or gives an error, tell me in the comments of this answer and I'll fix my code.

local part = script.Parent
local debounce = false
part.Touched:Connect(function(touched)
	local hum = touched.Parent:FindFirstChild("Humanoid") --if its a player
	if hum then
		if debounce == false then
			hum:TakeDamage(50) --Damage you want to do
			wait(0.5) --wait whatever amount of seconds
			debounce = true
		end
	end
end)

**EDIT: you can make it the part's transparency 1 and cancollide false to make it sort of an invisible "damage zone"

Make sure it's a server script and inside of your part. Hope this works :)

6 comments

0

That didn't work

0

Errors?

0

yes

0

as a server script do you mean a normal script, and it didn't work when you stoped moving

0

He's asking for the errors, not if you got errors.

0

no errors

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