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

How to load the only character who haven't loaded yet for at least a second?

Asked by
CjayPlyz 643 Moderation Voter
5 years ago
Edited 5 years ago
local debounce = false

local function Touched (hit)
    if debounce == false then
    debounce = true
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    player:LoadCharacter()
    wait(1)
    debounce = false
end

script.Parent.Touched:Connect(Touched)

Does someone know how i could make this script so that only players who haven't touched the part for at least a second could load his/her character and those who have touched it before the one second timeout have to wait?

0
So you want players who have not touched the part to be the ones getting loaded, but only after one second has passed when being touched? You can insert the players who have touched into a table for this. Then loop through all the ones who haven't and load them. xPolarium 1388 — 5y
0
That's what I understood from your explanation. Let me know if that's not what you meant. xPolarium 1388 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
local touched = {};
local Part = workspace.Part;
local Players = game:GetService("Players");


function isInTable(val, tab) 
    for i,v in pairs(tab) do
        if val == v then return true end
    end
    return false
end

function removeFromTable (val, tab)
    local newTab = {};

    for i,v in pairs(tab) do
        if v ~= val then 
            table.insert(newTab, val)
        end
    end

    return newTab
end

function getNotTouchedPlayers ()
    local notTouched = {};

    for num, player in pairs(Players:GetPlayers()) do 
        local hasTouched = isInTable(player, touched)
        if not hasTouched then table.insert(notTouched, touchedPlayer) end
    end

    return notTouched
end

Part.Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
        local Player = Players:GetPlayerFromCharacter(hit.Parent)
        if not Player then return end
        if isInTable(Player, touched) return end

        table.insert(touched, Player)

        delay(1, function()
            touched = removeFromTable(Player, touched)
        end)
    end
end)

spawn(function()
    while true do
        wait(1)
        local notTouching = getNotTouchedPlayers()
        for i,v in pairs (notTouching) do
            v:LoadCharacter()
        end
    end
end)
0
Just tried it, and it works :D thanks maybe next time try to use comments. CjayPlyz 643 — 5y
Ad

Answer this question