I’ve been trying to develop a system where when you hit a block at the end of an obby I’m making, you gain 1 completion and it sends you back to spawn (That part is working, it’s in a separate script to save me a headache)
But, when the player hits it, the script adds way too many points (usually 2-3), I know the problem (It’s adding everyone collision at once), just not the solution
Hello Alex!
As D0RYU said, you can use debounce to prevent the Touched event from firing too many times. Also check if the Added Instance is a player or not:
if game.Players:GetPlayerFromCharacter(Added.Parent) then
-- rest of the script
or
if Added.Parent.Humanoid then
-- rest of the script
There are a few errors in your script. I’ve fixed them and attached brief explanations in the diff below.
+ local debounce = false -- debounce variable
game.Players.PlayerAdded:Connect(function(Player)
- local leaderstats = Instance.new("Folder",Player) -- Using Instance.new() with parent argument isn't recommended
+ local leaderstats = Instance.new("Folder")
+ leaderstats.Parent = Player
leaderstats.Name = "leaderstats"
- local Complete = Instance.new("IntValue",leaderstats)
+ local Complete = Instance.new("IntValue")
+ Complete.Parent = leaderstats
Complete.Name = "Completions"
Complete.Value = 0
- script.Parent.Touched:connect(function(Added)
+ script.Parent.Touched:Connect(function(touchedPart) -- "connect" is deprecated
+ if touchedPart.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(touchedPart.Parent) == Player and debounce == false then debounce = true -- debounce and check to see if touchedPart is a player
- Complete.Value = Complete.Value + 1
+ Complete.Value += 1 -- addition assignment operator
+ debounce = false
end
end)
end)
Additionally, it’s good to turn your PlayerAdded/PlayerRemoving events into functions, because players sometimes load before the scripts do.
local function playerAdded(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = Player
leaderstats.Name = "leaderstats"
local Complete = Instance.new("IntValue")
Complete.Parent = leaderstats
Complete.Name = "Completions"
Complete.Value = 0
script.Parent.Touched:Connect(function(touchedPart)
if touchedPart.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(touchedPart.Parent) == Player and debounce == false then debounce = true
Complete.Value += 1
debounce = false
end
end)
end
for _,v in ipairs(game.Players:GetPlayers()) do -- in case they load in too fast
playerAdded(v)
end
game.Players.PlayerAdded:Connect(playerAdded)
Lastly, I highly advise against using this script in more than one part, otherwise there will be tons of leaderstats folders in a single player and too many unneeded events. I would put all the desired parts in a folder or group, then loop through that group simultaneously, listening for the touched event.
local cooldown = 5
local debounce = false
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
player.leaderstats.Completions.Value += 1
wait(cooldown)
debounce = false
end
end)