Help with staff door script

Hello I have an issue with the script when I put more than one id in local Admins = {1012904234, 701717374, 1337334373} it doesn’t work, it only works with one id but I want to put more ids because I have more admins so is there any way to fix it please? I don’t know how to fix it if someone know how please tell me thanks!

Script:

local Door = game.Workspace.AdminDoor
local MemoryStoreService = game:GetService("MemoryStoreService")
local SortedMap = MemoryStoreService:GetSortedMap("BansStorage")
local BanTime = 130
local Admins = {1012904234, 701717374, 1337334373}

local function SecondsToString(seconds)
	local days = math.floor(seconds / 86400)
	local hours = math.floor(seconds % 86400 / 3600)
	local minutes = math.floor(seconds % 3600 / 60)
	seconds = math.floor(seconds % 60)
	return string.format("%d Days, %02d Hours, %02d Minutes", days, hours, minutes)
end

local banTimeText = SecondsToString(BanTime)

Door.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	for i,v in pairs(Admins) do
		if Player.UserId == v then
print("Is Admin")
		else
	if Player == nil then return end
	local success, value = pcall(SortedMap.SetAsync, SortedMap, Player.UserId, os.time() + BanTime, BanTime)
	Player:Kick("You are currently banned. Time Left: " .. banTimeText)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(SortedMap.GetAsync, SortedMap, player.UserId)
	if success == false then return end
	if value == nil then return end
	local deltaTime = value - os.time()
	local timeText = SecondsToString(deltaTime)
	player:Kick("You are currently banned. Time Left: " .. timeText)
end)
1 Like

The for-loop needs to loop through the whole list of admins to check if the player is not an admin if you’re going to use else.

A better solution would be to return if the player is an admin, and put everything in the else statment after the for-loop.

For example:

for i, v in pairs(Admins) do
	if Player.UserId == v then
		print("Is an Admin")
		return
	end
end

print("Not an admin")

Your problem is that the script is looping through every id in the Admins table, but if the player’s userId is not the current value in the table then it would ban them. It doesn’t matter what order the table is in or how many items it has, the script would still go through the whole table and check whether the value is the userId or not. Basically, it would run the script inside the loop three times, or for however many items are in the table, and check wether the player’s userId is that value. You can check whether the player is an admin by using table.find(Admin, Player.UserId) or just by looping through the Admins and returning if the player is an Admin (if the loop doesn’t return, just run the ban code after it.)

Script
local Door = game.Workspace:WaitForChild("AdminDoor")
local MemoryStoreService = game:GetService("MemoryStoreService")
local Players = game:GetService("Players")
local SortedMap = MemoryStoreService:GetSortedMap("BansStorage")
local BanTime = 130
local Admins = {1012904234, 701717374, 1337334373}

local function SecondsToString(seconds)
	local days = math.floor(seconds / 86400)
	local hours = math.floor(seconds % 86400 / 3600)
	local minutes = math.floor(seconds % 3600 / 60)
	seconds = math.floor(seconds % 60)
	return string.format("%d Days, %02d Hours, %02d Minutes", days, hours, minutes)
end

local function isAdmin(Player)
	if table.find(Admins, Player.UserId) then
		return true
	end
end

local banTimeText = SecondsToString(BanTime)

Door.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	if Player then
		if isAdmin(Player) then
			print(Player, "is an Admin.")
		else
			local Success, Response = pcall(SortedMap.SetAsync, SortedMap, Player.USerId, os.time() + BanTime, BanTime)
			Player:Kick("You are currently banned. Time left: " .. banTimeText)
		end
	end
end)

Players.PlayerAdded:Connect(function(Player)
	local Success, Response = pcall(SortedMap.GetAsync, SortedMap, Player.UserId)
	if not Success then return end
	if Response then
		local deltaTime = Response - os.time()
		local timeText = SecondsToString(deltaTime)
		Player:Kick("You are currently banned. Time left: " .. timeText)
	end
end)
1 Like