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

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

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?

2 comments

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.

0

That's what I understood from your explanation. Let me know if that's not what you meant.

1 answer

✓ Accepted answer
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)

1 comment

0

Just tried it, and it works :D thanks maybe next time try to use comments.

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