Need help implementing a debounce table!

Like i mention in the title, I really need help implementing an effective debounce table.
My current issue is that, When the part is touched by multiple people at the same time it breaks and it no longer works but it prints debugs.

Here is my code, dont worry everything else is defined correctly, just need guidance on the debounce table.

local playerDebounceTable = {}

Part.Touched:Connect(function(Touched)
	local character = Touched.Parent
	if character:IsA("Model") then
		local Player = game.Players:GetPlayerFromCharacter(character)
		if Player and Player:IsA("Player") then
			if not playerDebounceTable[Player] then
				playerDebounceTable[Player] = false 
			end

			if not playerDebounceTable[Player] then
				playerDebounceTable[Player] = true 

				print("Touched by player:", Player.Name)

				local Data = DataCache.GetDataFrom(Player)
				local OwnedVIP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 244704218)
				local OwnedRP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 245347083)

				if Player.Data.Rarity1.Value >= Config.RarityReq.Value then
					if OwnedVIP and OwnedRP then
						Data.RebirthPoints += 4
					elseif OwnedRP then
						Data.RebirthPoints += 3
					elseif OwnedVIP then 
						Data.RebirthPoints += 2
					else
						Data.RebirthPoints += 1
					end

					Player.Data.RebirthPoints.Value = Data.RebirthPoints
					Data.Rarity = 0
					Player.Data.Rarity.Value = Data.Rarity
					Data.RebirthReq += 1
					Player.Data.RebirthReq.Value = Data.RebirthReq
					Config.RarityReq.Value = Player.Data.RebirthReq.Value 
				end

				wait(1)
				playerDebounceTable[Player] = false 
			end
		end
	end
end)```

Note: This requires some rewriting. Unless you want to do this, ignore this.

Remove this, you’re already defining the debounce.

It’s better if you use Player.Name instead of Player.

Main problem

The touched event can only handle a part at a time. This causes the script to focus on only one player ( and repeatably ) check if X player exists in the table. This is normal, and because it's handled by the server. It has to handle by it self.

Solution

Instead, use a local script stored in whatever place. Then make the script check if the local player is touching the part. After that, do whatever you want.

I’d recommend using Tags.

Part.Touched:Connect(function(Touched)
	local character = Touched.Parent
	if character:IsA("Model") then
		local Player = game.Players:GetPlayerFromCharacter(character)
		if Player and Player:IsA("Player") then
			if Player:HasTag("COOLDOWNTOUCHED") then
				return
			end
			if not Player:HasTag("COOLDOWNTOUCHED") then
				Player:AddTag("COOLDOWNTOUCHED")
				task.delay(1,function()
					Player:RemoveTag("COOLDOWNTOUCHED")
				end)
				print("Touched by player:", Player.Name)

				local Data = DataCache.GetDataFrom(Player)
				local OwnedVIP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 244704218)
				local OwnedRP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, 245347083)

				if Player.Data.Rarity1.Value >= Config.RarityReq.Value then
					if OwnedVIP and OwnedRP then
						Data.RebirthPoints += 4
					elseif OwnedRP then
						Data.RebirthPoints += 3
					elseif OwnedVIP then 
						Data.RebirthPoints += 2
					else
						Data.RebirthPoints += 1
					end

					Player.Data.RebirthPoints.Value = Data.RebirthPoints
					Data.Rarity = 0
					Player.Data.Rarity.Value = Data.Rarity
					Data.RebirthReq += 1
					Player.Data.RebirthReq.Value = Data.RebirthReq
					Config.RarityReq.Value = Player.Data.RebirthReq.Value 
				end
			end
		end
	end
end)```
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.