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 8 years ago

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

function lava(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
    humanoid.Health = -10
    end
end
script.Parent.Touched:connect(lava)

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 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


--Define the debounce
local db = false

function lava(hit)
--Check the debounce
    if not db then
        --Set the debounce
        db = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid ~= nil then
            humanoid.Health = humanoid.Health -10
        end
        --Wait 1 second
        wait(1)
        --Disable debounce.
        db = false
    end
end

script.Parent.Touched:connect(lava)
Ad
Log in to vote
0
Answered by 8 years ago

Your problem is this line

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

Instead you need to do this:

humanoid.Health = humanoid.Health - 10

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

local debounce = false
function lava(hit)
if debounce then return end -- If the person has taken damage recently (see wait(1)) then don't take it again
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
    debounce = true
    humanoid.Health =  humanoid.Health  - 10
    wait(1) -- I'd also add a wait here
    end
end
script.Parent.Touched:connect(lava)

Hope this helped.

Answer this question