My script is firing too much

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

    game.Players.PlayerAdded:Connect(function(Player)

       local leaderstats = Instance.new("Folder",Player)
       leaderstats.Name = "leaderstats"

       local Complete = Instance.new("IntValue",leaderstats)
       Complete.Name = "Completions"
       Complete.Value = 0

       script.Parent.Touched:connect(function(Added)
	       Complete.Value = Complete.Value + 1
       end)	
   end)

how you ever heard of the term debounce before?

hope this helps

EDIT: oh and just a quick note :connect is deprecated, please use :Connect

1 Like

I’ve tried with debouncing, it’s being stupid and still adding them on

thanks for the link at least, might clear up some stuff

Edit: Thanks, I’ve just gotten back to scripting in LUA so I’m not super up to date with the syntax :slight_smile:

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

Oh, good idea to check for that, I’m testing out debouncing right now

one last thing I would like to mention to help shorten your code

Complete.Value = Complete.Value + 1
Complete.Value += 1

those work the same way

2 Likes

Yeah, fair, but in my mind Complete.Value + 1 is easier to remember. Sorry if it annoys you :pensive:

Just a guess, but I think they’re just messing around lol.

“Nonce” is british slang for p*do

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.

Good luck!

1 Like

Oh god, I’ve been told otherwise. I think I needa change that ahaha!

Use a debounce.

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)
1 Like

I was infact just messing around. Also, tab doesnt work, and IM NOT FORMATTING WITH SPACES

After a bit of tweaking, your version has worked the best for me

thanks :slight_smile: