Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Should I use debounce?[Solved]

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.

3 comments

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.

0

Its actually a block attached to an NPC, I dont exactly know how to use debounce yet, could you demonstrate for me?

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.

2 answers

✓ Accepted answer

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

3 comments

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?

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.

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.

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

4 comments

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

0

This script is broken. There is a bug in it.

0

I wasn't scanning for bugs or errors/problems. Sorry 'bout that.

0

You need one more "end" at the end of the function to close off "if human ~= nil then".

This discussion is preserved as it was. New answers, comments, and votes are closed.