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

When Player Touches Block, Only +1 Value added? [closed]

Asked by
GentiRob -81
5 years ago

Everything works but, When player touches it, as much steps as player takes value is added, I want to do 1 per 5 seconds!

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("BoolValue", plr)
    stats.Name = "leaderstats"

    local wins = Instance.new("IntValue", stats)
    wins.Name = "Wins"
    wins.Value = 0

workspace.Lobby.Winner.Touched:Connect(function()
    wins.Value = wins.Value + 1
end)

end)

0
I need more context, what are you trying to do? GoldAngelInDisguise 297 — 5y

Closed as Non-Descriptive by User#23365, User#24403, and BenSBk

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

2 answers

Log in to vote
0
Answered by
luadotorg 194
5 years ago

Hey GentiRob, before I move on to the answer to your problem I'd like to suggest you to explain your problem better: The problem/issue is a fundamental part of Scripting Helpers, and if you do not provide us with that, it's impossible for us to figure out what you mean.

I believe I understand what you mean, but it's really vague.

From what you've wrote, I feel like you're encountering an usual problem for beginners when it comes to .Touched: you're basing your code about it, which is usually a bad solution.

I'd suggest changing that so when you teleport the player to the Winner brick, if that is what you're doing, change the Wins value on that situation.

You can do that with this simple step:

-- rest of the code here

player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1

-- teleport code here

Now; to prevent the user from getting a Win every time they touch the Winner brick, all you have to do is add a debounce.

But what is a debounce? Basically, you're encountering a problem where there is no cooldown or limit to how many wins the player can get. A debounce fixes that issue, as it acts like a cool down. It seems pretty simple, but it is very efficient and a very common practice.

Example situation:

local Debounce = false

game:GetService("Workspace").Baseplate.Touched:Connect(function(part)
    Debounce = not Debounce -- The "not" keyword negates the value (false -> true, true -> false)

    if Debounce then
        print("The Baseplate has been touched!")
    end
end)

This example code will stop the code from being ran every second step, which means less output spam!

Implementing a timed debounce There are multiple ways you can add a timed debounce.

I'm going to present you with a very useful and common function: tick()

https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Roblox#tick

Returns how much time has elapsed since the UNIX epoch, on the current local session’s computer. The UNIX epoch is represented by the date January 1st, 1970.

You can make a debounce use tick() by simply making the debounce a numeric value that is obtained through tick() itself!

Here's the solution to your problem, using a timed debounce:

local Debounce = tick() -- Assign tick() to the variable

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("BoolValue", plr)
    stats.Name = "leaderstats"

    local wins = Instance.new("IntValue", stats)
    wins.Name = "Wins"
    wins.Value = 0

workspace.Lobby.Winner.Touched:Connect(function()
    if (tick() - Debounce) >= 5 then -- Make sure 5 seconds have elapsed since last debounce
        Debounce = tick()

            wins.Value = wins.Value + 1
    end
end)

end)

Hope this helps you out!

0
Thank You So Much! Have a Good Day :D GentiRob -81 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago
game.Players.PlayerAdded:Connect(function(plr)
02      local stats = Instance.new("BoolValue", plr)
03      stats.Name = "leaderstats"
04   
05      local wins = Instance.new("IntValue", stats)
06      wins.Name = "Wins"
07      wins.Value = 0
08   
09  workspace.Lobby.Winner.Touched:Connect(function()
-   while wait(5) do
10      wins.Value = wins.Value + 1
-   end
11  end)
12  end)

Is this what you needed? If not then please be more clear about your question.

0
First of all, no explanation is given. Second of all, this does not address the problem, it just makes a worse problem happen. User#25115 0 — 5y