2 Ways, either add a debounce or remove and bring back the part (Remove does not fully get rid of the object. The object still exists when you use remove).
#Debounce
A debounce is a way to stop a script from going for a moment and then letting it run after a certain amount of time. Look at this example from the Wiki.
Workspace.Button.Touched:connect(function(hit)
print("Button pressed")
wait(1)
print("Hi :D")
end)
Output you expect:
Button pressed
Hi :D
Actual Output:
Button pressed
Button pressed
Button pressed
Button pressed
Button pressed
Hi :D
Hi :D
Hi :D
Hi :D
Hi :D
Fixed code:
local debounce = false
Workspace.Button.Touched:connect(function(hit)
if not debounce then
debounce = true
print("Button pressed")
wait(1)
print("Hi :D")
debounce = false
end
end)
Output:
Button pressed
Hi :D
#Remove
Remove is a way to get rid of objects, but the problem is it doesn't actually remove the parts. It just sets the parent to nil, meaning it can be returned. This is why I use Destroy instead. Anyway, using the knowledge that remove doesn't completely remove the part, we can exploit this in our favor.
local part = workspace.Part
part:remove()
wait(3)
part.Parent = workspace
Still, use Destroy instead for your primary way of getting rid of objects.
#Final Product
These are the different ways of doing this:
##Debounce
local debounce = false
script.Parent.Touched:connect(function()
if not debounce then
debounce = true
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
wait(5)
end
end
debounce = false
end
end)
##Debounce+Invisible part
local debounce = false
script.Parent.Touched:connect(function()
if not debounce then
debounce = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
wait(5)
end
end
debounce = false
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
end)
##Remove/Parent-To-nil
local part = script.Parent
local parent = part.Parent
part.Touched:connect(function()
for _,Player in pairs(game.Players:GetPlayers()) do
if Player:FindFirstChild("leaderstats") then
Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
end
end
part:remove()
wait(3)
part.Parent = parent
end)
Hope it helps!
1 comment
first you should clone it