I have this brick that is on fire it is supposed to burn the player and hurt the player over time
heres the script
1 | function onTouched(part) |
2 | local h = part.Parent:FindFirstChild( "Humanoid" ) |
3 | if h ~ = nil then |
4 | h.Health = h.Health - 1 |
5 | end |
6 | end |
7 |
8 | script.Parent.Touched:connect(onTouched) |
But when the player isn't touching the brick it will not harm the player
to get this straight too I the player touches the brick it will harm them bt as soon as the stop touching the brick it will stop harming them
https://www.roblox.com/games/540624890/2D-Adventure-NEW-RELEASE
To do what you want, make sure the part's being touched and while it's being touched do the code,
01 | local part = script.Parent |
02 |
03 | local function Check(c) |
04 | local parts = c:GetChildren() |
05 | local touching = part:GetTouchingParts() --Might be name |
06 | for i,v in next , touching do |
07 | for i 2 ,v 2 in next , parts do |
08 | if v 2 :IsA( "BasePart" ) then |
09 | if v 2 = = v then |
10 | return true ; |
11 | end |
12 | end |
13 | end |
14 | end |
15 | return false ; |
One way to do so is to make a for
loop. We can get the player's health and make a decrement for
loop.
Here is how it's done!
01 | local db = false |
02 | local dbDelay = 3 |
03 |
04 | function onTouched(part) |
05 | local h = part.Parent:FindFirstChild( "Humanoid" ) |
06 | if db = = true then |
07 | return |
08 | else |
09 | if h ~ = nil then |
10 | db = true |
11 | local count = 0 |
12 | for i = h.Health, 0 , - 1 do |
13 | h.Health = h.Health - 1 -- Subtracts the player's health by 1 every second. The number of times is based on the player's health. |
14 | count = count + 1 |
15 | if count = = dbDelay then |
The debounce is very useful in order to avoid health decreasing too quickly. That's all I have in mind. If you have any problems, please leave a comment below. Thank you and I hope this will help you.
As you said in your comment, you want to subtract 1 health every second,
there are two main ways you can achieve this:
01 | function onTouched(part) |
02 | local h = part.Parent:FindFirstChild( "Humanoid" ) |
03 | if h ~ = nil then |
04 | while true do --sorry if I messed up on the while true do loop, haven't touched lua in a while |
05 | h.Health = h.Health - 1 |
06 | wait( 1 ) |
07 | end |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:connect(onTouched) |
01 | function onTouched(part) |
02 | local h = part.Parent:FindFirstChild( "Humanoid" ) |
03 | if h ~ = nil then |
04 | for = 100 , 0 , - 1 do --sorry if I messed up how on the for loop, haven't touched lua in a while |
05 | h.Health = i |
06 | wait( 1 ) |
07 | end |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:connect(onTouched) |
Any questions, comments or concerns? Put them in a comment and I'll answer them when I get it!
If I did anything wrong, please put it down in the comments and I'll fix it :)
Are there any better methods to doing this? Make sure to comment to inform us all :)