My touched events are firing to much leading to player getting to many points

Im also using this blacklistt players system for the touched events someone suggested but still not working

This is of a thing where the player can only claim points after a certain amount of time

Here is my current script:

   local DataStoreServce = game:GetService("DataStoreService")

local DebrisService = game:GetService("Debris")

local DataStore = DataStoreServce:GetDataStore("lo1_229112")

local TimeForWait = 5 --//btw this is in scends


function Set(Player)
	local s,e = pcall(function()
		DataStore:SetAsync(Player.UserId, os.time())
	end)
end


game.Players.PlayerRemoving:Connect(function(Player)
	
	local CurrentTime = os.time()
	
	--print(CurrentTime)
	
	Set(Player)
	
end)

local function TeleportBack(player)
	player.Character.HumanoidRootPart.Position =  game.Workspace.Lobby.BackToSpawn.Position
end


local blacklist = {};

function foundInList(player)
	for _,target in ipairs(blacklist) do
		if target == player then
			return true;
		end
	end
	return false;
end

local function UnBlackListPlayer(player)
	for i,v in ipairs(blacklist) do
		if v.Name == player.Name then
			table.remove(blacklist, i)
			print('cool')
		end
	end
end



script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		for i,v in pairs(blacklist)
		do
			print(v)
		end
		
			if player then
				
				if not foundInList(player) then
					
				table.insert(blacklist,player);
		
				local Data = DataStore:GetAsync(player.UserId)
				local Points = player.leaderstats.Points
			
				if Data == nil then
					print("Player can get points")
					
					Points.Value = Points.Value + 500
					--TeleportBack(player)
					Set(player)
					--wait(1)
				elseif os.time() - Data >= TimeForWait then
					print("HWTA")
					
					Points.Value = Points.Value + 500
					--TeleportBack(player)
					Set(player)
					
				else
					print("Player claimed before")
				end
	
				--print("Old data is " .. Data .. " new data is .. ".. os.time())
			
			--print("its been ".. os.time() - Data.. " seconds")
		end
		end
		wait(5)
		UnBlackListPlayer(player)
	end
end)


game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		Set(v)
	end
end)
1 Like

You should add a debounce to your touched event. Your teleport event is not going to instantly teleport players away, and a player may be able to get additional touches on the brick before teleportation. A debounce is simply a boolean that’s used to filter out repetitive touches. Here’s example code of one:

local debounce = false

script.Parent.Touched:Connect(function(hit)
    if not debounce then     --same as if debounce == false
        debounce = true
        --Do fancy stuff
        wait(3)    --Substitute 3 for how often you want your part touched
        debounce = false
    end
end)

What this code will do is basically filter out touches for 3 seconds once it is touched. Once three seconds are up, it will accept touches again. Hopefully this helps you with your problem.

But , debounces woudlnt be specfic for a player, the denounce would do it for all players

Correct me if Im wrong, thats what people said on my topic I posted before

You are correct, but there is a fancy way to do debounces that are specific to each player. All you simply need is a table to keep track of the humanoids you hit, and manage their debounces seperately, like this:

local hitHums = {}

script.Parent.Touched:Connect(function(hit) 
    local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
    if hit and hit.Parent and hum and hum.Health > 0 and not hitHums[hum] then
        hitHums[hum] = true
        --Do fancy stuff :)
        wait(3)
        hitHums[hum] = false
    end
end)

This basically takes advantage of lua dictionaries, where we are using the player’s humanoid as the key and that key is used as their own personal debounce! I used this method all the time for hitboxes on my weapons.

1 Like

That seems a little complciated lol but yah thanks, I will try that today