Hi I have tried to make a script that every second you are on the brick you take 10 damage.
1 | function lava(hit) |
2 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
3 | if humanoid ~ = nil then |
4 | humanoid.Health = - 10 |
5 | end |
6 | end |
7 | script.Parent.Touched:connect(lava) |
With roblox physics, you don't only touch an object once.. you "bounce" on the object; therefore, touching it 5, 6, 7, etc.. times. Due to this, often times a Debounce is set up.
A debounce essentially prevents the code from firing multiple times, even if the event is fired multiple times. A debounce makes it so you check a value before continuing the code. If this value is false then turn it true and do the code. If it's true then do nothing.
Set up a debounce in your code, and disable it after one second.
Also, on line 4, you were just setting the health value to -10. To subtract 10 health you need to set the value relatively to itself. So you set the health value to the health value minus 10; health = health - 10
01 | --Define the debounce |
02 | local db = false |
03 |
04 | function lava(hit) |
05 | --Check the debounce |
06 | if not db then |
07 | --Set the debounce |
08 | db = true |
09 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
10 | if humanoid ~ = nil then |
11 | humanoid.Health = humanoid.Health - 10 |
12 | end |
13 | --Wait 1 second |
14 | wait( 1 ) |
15 | --Disable debounce. |
16 | db = false |
17 | end |
18 | end |
19 |
20 | script.Parent.Touched:connect(lava) |
Your problem is this line
1 | humanoid.Health = - 10 -- This just sets the health to -10 |
Instead you need to do this:
1 | humanoid.Health = humanoid.Health - 10 |
Also, as the function will be continuously called, you'll need a debounce in there.
01 | local debounce = false |
02 | function lava(hit) |
03 | if debounce then return end -- If the person has taken damage recently (see wait(1)) then don't take it again |
04 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if humanoid ~ = nil then |
06 | debounce = true |
07 | humanoid.Health = humanoid.Health - 10 |
08 | wait( 1 ) -- I'd also add a wait here |
09 | end |
10 | end |
11 | script.Parent.Touched:connect(lava) |
Hope this helped.