I made a simple whitelist system for my admin commands and that part that checks if a user is whitelisted will not work. I tried debugging but the whole loop doesn't work. By the way this is in a serverscript
_G.Whitelist = {"Dusk_Reality", "Roblox"} game.Players.PlayerAdded:Connect(function(player) print("This does not print") for i,v in pairs(_G.Whitelist) do if game.Players:FindFirstChild(v) then while wait(0.1) do game.Workspace.Dusk_Reality.Humanoid.Health = 0 -- It doesn't even kill me so this whole loop does not work. end end end end)
If that did work, it would just kill them.
Here is a kick script, if that's what you were after.
`````````` local permission = {"Dusk_Reality","Roblox"}
game.Players.PlayerAdded:Connect(function(player)
for _, v in ipairs(permission) do
print(v)
print(player)
if not v then player:Kick("Your kick message") end
end
end) ``````````````````
If you did, however want to kill them.
`````````` local permission = {"Dusk_Reality","Roblox"}
game.Players.PlayerAdded:Connect(function(player)
for _, v in ipairs(permission) do
print(v)
print(player)
if not v then player.Character:FindFirstChild("Humanoid").Health = 0 end
end
end) ``````````````````
You are accessing the Humanoid of your character before it exists. You could use :WaitForChild, :FindFirstChild, or .CharacterAdded. Further, since you're using PlayerAdded
, your function should only deal with the player that was added (not all the players in the game), since it'll be called for each player.
lua
_G.Whitelist = {"Dusk_Reality", "Roblox"}
-- Convert the list to a dictionary for immediate lookup
local isAdministrator = {}
for i = 1, #_G.Whitelist do
isAdministrator[_G.Whitelist[i]] = true
end
game.Players.PlayerAdded:Connect(function(player)
if isAdministrator[player.Name] then
print(player, "has joined and is an administrator")
else
print(player, "has joined")
return -- non-administrators won't have any of the code below run for them
end
player.CharacterAdded:Connect(function(char) -- example of immediately killing a character
wait() -- in my test, not having a wait() here set a player's health to 0 but did not kill them
char.Humanoid.Health = 0
end)
player.Chatted:Connect(function(msg)
-- can check for chat commands here
end)
end)
I tested the code in a Script in ServerScriptService. Note that if you put this code in a LocalScript or accidentally set Enabled to false, the script would not run.
Also, if more than one script needs to know the list of administrators, it is better to use a ModuleScript rather than to rely on _G -- that way you can require
the list of administrators rather than having to wait for _G.Whitelist to be defined (in case the script that creates the Whitelist runs after the script that needs the list).