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

My touched event adds a point for each limb?

Asked by 6 years ago

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:

01--Varibles / Settings
02local HoleValue = 1
03 
04--Script
05script.Parent.Touched:Connect(function(hit)
06    --Checks
07    if hit.Parent:WaitForChild("Humanoid") then
08        --Varibles
09        local PlayerName = hit.Parent.Name
10        local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
11        --Kill
12        hit.Parent.Humanoid.Health = 0
13        local EsimateValue = HoleValue * Multiplier
14        game.Players[PlayerName].leaderstats.Oofs.Value = game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
15    end
16end)
0
just use a debounce DinozCreates 1070 — 6y
0
That alone wouldn't work, as when the player dies, they could stay on the pad and it would repeat turtle2004 167 — 6y
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. DinozCreates 1070 — 6y
0
i also assumed he knew what a debounce was, and i probably shouldn't have, good explanation turtle! DinozCreates 1070 — 6y
0
Thank you, although I always feel like I'm spoonfeeding or giving false info, I annoy myself xD turtle2004 167 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

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.

1--Varibles / Settings
2local HoleValue = 1
3local 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.

01script.Parent.Touched:Connect(function(hit)
02    if not debounce then --If debounce is false, the statement is true. The not operator reverses the statement
03 
04     if hit.Parent:WaitForChild("Humanoid") then
05           local PlayerName = hit.Parent.Name
06            local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
07           hit.Parent.Humanoid.Health = 0
08            local EsimateValue = HoleValue * Multiplier
09          game.Players[PlayerName].leaderstats.Oofs.Value
10game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
11    end
12end)

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.

01--Script
02script.Parent.Touched:Connect(function(hit)
03    if not debounce then
04        debounce = true --Switch its state to prevent the function from running twice.
05    --Checks
06    if hit.Parent:WaitForChild("Humanoid") then
07        --Varibles
08        local PlayerName = hit.Parent.Name
09        local Multiplier = game.Players[PlayerName].leaderstats.Multipliers.Value
10        --Kill
11        hit.Parent.Humanoid.Health = 0
12        local EsimateValue = HoleValue * Multiplier
13        game.Players[PlayerName].leaderstats.Oofs.Value = game.Players[PlayerName].leaderstats.Oofs.Value + EsimateValue
14    debounce = false --Set it back to its default state
15    end
16end)

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

Ad

Answer this question