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

Should I use debounce?[Solved]

Asked by 9 years ago

I'm wondering if I'm supposed to use debounce to temporarily disable the damage script, just so that 10 damage doesnt = 30 damage when you hit it 3 times too quickly. Also I'm wondering why I cant copy stuff from ROBLOX to my clipboard, it almost seems like there is a clipboard just for ROBLOX and I couldn't just copy and paste to post this question.

Damage = 10
function onTouch(hit)
human = hit.Parent:FindFirstChild("Humanoid")
if human ~=nil and human.Parent.color~=nil then
script.Parent.Parent.Humanoid.Jump = true
human.Health = human.Health - Damage
end
end

script.Parent.Touched:connect(onTouch)

ANY help is appreciated (Videos and Wiki links too) Thanks.

0
Yes, use a debounce. I assume this is just a platform to step or land on. If you don't have a debounce then you'll take damage multiple times. Try to make it maybe a second or so. alphawolvess 1784 — 9y
0
Its actually a block attached to an NPC, I dont exactly know how to use debounce yet, could you demonstrate for me? BSIncorporated 640 — 9y
0
Yes, this has been happening to me too. I just save the game, close the studio and reopen the studio and saved file. Also, accept this answer please. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

To use debounce, you would need a variable. We could call this variable debounce.

local debounce = false --Notice how this is a boolean value. It doesn't have to be a boolean. Booleans are simple and easy to use.

local debounce_2 = 0 --This is a number value. This would also work if used correctly.

How do they work? Well an If statement checks to see if the debounce is a certain value.

if not debounce then
    print("Foo")
end
--Both these will print "Foo". Also, you may want to do "if not debounce" if the debounce is false. As you can see the first debounce example is shorter.
if debounce == false then
    print("Foo")
end

if debounce_2 == 0 then
    print("Foo2")
end
--Both of these also print.
if debounce_2 < 1 then
    print("Foo2")
end

Now, denounces are supposed to prevent something from functioning more than once. This is how:

local debounce = false --Now the debounce is false

function printme(msg, time) --The message
    if not debounce then--Do this in anyway of your liking.
        debounce = true --This will prevent the message from spamming the output.
        print(msg) --Prints the message
        wait(time) --Wait time amount of seconds. In this case, it's 3.
        debounce = false --Turn the debounce to false to be used again.
    end
end

while wait() do --loop
    printme("Foo", 3) --fire the function "printme" with the msg of "Foo" and the time of 3 seconds.
end

Now about every 3.03 seconds you will get the message "Foo" in the output.


We can now add this to your script.

local debounce = false --You May make Debounce anything.
local Damage = 10
function onTouch(hit)
human = hit.Parent:FindFirstChild("Humanoid")
if not debounce and human ~=nil and human.Parent.color~=nil then Debounce = true
human.Jump = true --The variable human is the humanoid.
human.Health = human.Health:TakeDamage(Damage)
wait(3)--Wait 3 seconds.
debounce = false --Resets debounce.
end
end

script.Parent.Touched:connect(onTouch)

Now in this script, this uses some function called :TakeDamage(). This might work better because this means that the player can't take damage if it has a ForceField. If you don't want this to happen, just replace line 7 with human.Health = human.Health-Damage

0
Thanks, do you know anything about my clipboard problem? I tried re-installing ROBLOX Studio, then it worked, then it broke again...anything I can do? BSIncorporated 640 — 9y
0
Yes, this has been happening to me too. I just save the game, close the studio and reopen the studio and saved file. Also, accept this answer please. EzraNehemiah_TF2 3552 — 9y
0
Roblox is currently having problems. Many, many maintenance's have taken place. Problems still exist; I think they said that some thing will not respond at times. alphawolvess 1784 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

This is how your script will look like:

Debounce = false --You May make Debounce anything.
Damage = 10
function onTouch(hit)
human = hit.Parent:FindFirstChild("Humanoid")
if Debounce == false then Debounce = true
if human ~=nil and human.Parent.color~=nil then
script.Parent.Parent.Humanoid.Jump = true
human.Health = human.Health - Damage
wait(1)
Debounce = false
end
end

script.Parent.Touched:connect(onTouch)

Look at this webpage: http://wiki.roblox.com/index.php?title=Debounce

0
At the bottom you see wait(1) and on the next line, Debounce = false. Debounce currently is true, so touching it again (For Any Player) during this wait, the script will not do anything because the conditions are not met for if Debounce == false then alphawolvess 1784 — 9y
0
This script is broken. There is a bug in it. EzraNehemiah_TF2 3552 — 9y
0
I wasn't scanning for bugs or errors/problems. Sorry 'bout that. alphawolvess 1784 — 9y
0
You need one more "end" at the end of the function to close off "if human ~= nil then". EzraNehemiah_TF2 3552 — 9y

Answer this question