Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script gets executed multiple times how to fix this?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question