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

i want a kill brick to decrease heath not going back and forward, why is it doing this?[solved]

Asked by 10 years ago

I want to make a kill brick that decreases the players heath slowly. I try to script the killing but the first part of heath screws up, when I tested it my heath went back and forward between 90 and 80.

I want the heath to decrease not going back and forward, why is it doing this?

my script: function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if h~=nil then h.Health = 90 wait(5) h.Health = 80 wait(5) h.health = 70 wait(5) h.health = 60 wait(5) h.health = 30 wait(5) h.health = 15 wait(5) h.health = 7 wait(5) h.health = 0 end end script.Parent.Touched:connect(onTouched)

I don't know why its not working correctly

Please help me scriptinghelpers!

0
Please put your code inside of the two lines of squiggles (which should each be on their own line) created when click the Lua symbol on your text editor. It formats the code to make it easier to read. adark 5487 — 10y

2 answers

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

[Edited - I can't believe I didn't realize it I derped]

Your problem is that you need a debounce, otherwise the script will keep resetting everytime the player moves or 'bounces' on the part, and because of roblox physics, this 'bounceing' happens quite often; therefore, debounce.

db = false
touch = nil

script.Parent.Touched:connect(function(hit)
    if db == false then db = true end
    h = hit.Parent:FindFirstChild("Humanoid")
    if (h ~= nil) then
        touch = true
        while touch == true do
            h.Health = h.Health - 10
            wait(5)
        end
        script.Parent.TouchEnded:connect(function()
            touch = false
        end)
    end
end)

This would pretty much do the same thing as yours, debounce aside, just stop if they stopped touching the part and it reduces the health instead of setting it.

-Goulstem

1
You actually fixed the problem in the code, without noticing! The debounce you added fixed why his code had the health "bouncing back and forth", but your code adds multiple additional bugs, and doesn't guarantee a kill like the OP's code eventually does. adark 5487 — 10y
1
Yes, I just noticed after your post lol I can't believe i missed it. But I mean I doubt theres any want for someone to definitely die even if they're off the part. I assumed, which isn't a very good thing, but I hope it was good for him(: Goulstem 8144 — 10y
0
It's not a bad design choice, but it produces different results from what the OP seems to want, based on his original code. adark 5487 — 10y
Ad
Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

What's going on is that the Touched method of script.Parent is getting fired multiple times, so the function is being called multiple times, making it look like the health is "hiccuping" back and forth.

To fix this, I'm going to add a 'Dying' tag to the Humanoid, and check for it before doing damage:

function onTouched(part)
    local h = part.Parent:FindFirstChild("Humanoid") -- lowercase f `findFirstChild` is deprecated
    if h and not h:FindFirstChild("Dying") then -- `~= nil` is never necessary.
        Instance.new("IntValue", h).Name = "Dying"
        h.Health = 90
        wait(5)
        h.Health = 80
        wait(5)
        h.health = 70
        wait(5)
        h.health = 60
        wait(5)
        h.health = 30
        wait(5)
        h.health = 15
        wait(5)
        h.health = 7
        wait(5)
        h.health = 0
    end
end
script.Parent.Touched:connect(onTouched)

Answer this question