Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Standing and brick and taking more and more damage?

Asked by 9 years ago

Hi I have tried to make a script that every second you are on the brick you take 10 damage.

1function lava(hit)
2    local humanoid = hit.Parent:FindFirstChild("Humanoid")
3    if humanoid ~= nil then
4    humanoid.Health = -10
5    end
6end
7script.Parent.Touched:connect(lava)

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
02local db = false
03 
04function 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
18end
19 
20script.Parent.Touched:connect(lava)
Ad
Log in to vote
0
Answered by 9 years ago

Your problem is this line

1humanoid.Health = -10 -- This just sets the health to -10

Instead you need to do this:

1humanoid.Health = humanoid.Health - 10

Also, as the function will be continuously called, you'll need a debounce in there.

01local debounce = false
02function lava(hit)
03if 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
10end
11script.Parent.Touched:connect(lava)

Hope this helped.

Answer this question