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:

--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)
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.

--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

Ad

Answer this question