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:
player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
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
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()
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
Debounce = tick()
wins.Value = wins.Value + 1
end
end)
end)
Hope this helps you out!
1 comment
I need more context, what are you trying to do?