Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script gets executed multiple times how to fix this?

Hello there! I have just created a system kill everyone in my admin area but also to log it in my discord server the problem is that the code gets repeated 5 - 6 times in my discord. I've tried multiple sulutions like putting a wait but that doesn't work.

Here is my code:

local part = script.Parent;
local http = game:GetService("HttpService");
local Players = game:GetService("Players");

part.Touched:Connect ( function (hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid");
    if humanoid then
		local player = Players:GetPlayerFromCharacter(hit.Parent); 
		player:LoadCharacter()
		local Data = {
			["content"] = player.Name.." was detected in the admin system!"
		}
		Data = http:JSONEncode(Data)
		http:PostAsync("NOT IMPORTANT!", Data)
	end
end)

1 answer

✓ Accepted answer

add debounce.. rewrite it like this:


local part = script.Parent;
local http = game:GetService("HttpService");
local Players = game:GetService("Players");
local db = false

part.Touched:Connect ( function (hit)
	if(db) then return end
	db = true

   	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid");
    	if humanoid then
      		 local player = Players:GetPlayerFromCharacter(hit.Parent); 
        	player:LoadCharacter()
       	 	local Data = {
            		["content"] = player.Name.." was detected in the admin system!"
        	}
        	Data = http:JSONEncode(Data)
        	http:PostAsync("NOT IMPORTANT!", Data)
    	end

	wait(2)
	db= false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.