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

My touched event adds a point for each limb?

How can I make this only add one point to the oof leaderboard instead of adding ranged from 2 - 15 amount of points? I think it's counting every limb?

Any way to make it only add one point?

Script:

--Varibles / Settings
local HoleValue = 1

--Script
script.Parent.Touched:Connect(function(hit)
	--Checks
	if hit.Parent:WaitForChild("Humanoid") then
		--Varibles
		local PlayerName = hit.Parent.Name
		local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
		--Kill
		hit.Parent.Humanoid.Health = 0
		local EsimateValue = HoleValue * Multiplier
		game.Players[PlayerName].leaderstats.Oofs.Value = game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
	end
end)

5 comments

0

just use a debounce

0

That alone wouldn't work, as when the player dies, they could stay on the pad and it would repeat

0

pad? i read the code as you gaining a point for killing "something", so i figure just adding a db/delay would allow the enemy to die.

0

i also assumed he knew what a debounce was, and i probably shouldn't have, good explanation turtle!

0

Thank you, although I always feel like I'm spoonfeeding or giving false info, I annoy myself xD

1 answer

✓ Accepted answer

As stated by Dino, you'd have to use a debounce. Basically, this stops a function from running too many times.

To implement it, you need to initialize a boolean value which in your case could be done directly below HoleValue. To avoid confusion we'll just call it debounce.

--Varibles / Settings
local HoleValue = 1
local debounce = false

Now, how do you use a debounce? You will have to use a conditional if statement at the start of your function to check if debounce is equal to false.

script.Parent.Touched:Connect(function(hit)
    if not debounce then --If debounce is false, the statement is true. The not operator reverses the statement

   	 if hit.Parent:WaitForChild("Humanoid") then
 	       local PlayerName = hit.Parent.Name
	        local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
 	       hit.Parent.Humanoid.Health = 0
	        local EsimateValue = HoleValue * Multiplier
  	      game.Players[PlayerName].leaderstats.Oofs.Value
game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
    end
end)

The next step is hopefully quite obvious. We need to set the debounce to true when we begin the function and false when it is finished.

--Script
script.Parent.Touched:Connect(function(hit)
    if not debounce then
        debounce = true --Switch its state to prevent the function from running twice.
    --Checks
    if hit.Parent:WaitForChild("Humanoid") then
        --Varibles
        local PlayerName = hit.Parent.Name
        local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
        --Kill
        hit.Parent.Humanoid.Health = 0
        local EsimateValue = HoleValue * Multiplier
        game.Players[PlayerName].leaderstats.Oofs.Value = game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
	debounce = false --Set it back to its default state
    end
end)

On top of this, you would need to add some sort of conditional as this alone won't stop multiple points being given. As I don't want to spoon feed you the answer, and I also do not want to give you false information, I'll give you some prompts:

Wait a few seconds before switching debounce to false Con: If two people touch it within that time, it will only work for the first

Store the player in a table, check if they're in it, wait for a period of time and then remove them Con: There are easier ways

I will try to give you a proper answer when i'm not so tired

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