I am trying to make a gun only give to specific people, but it just doesn’t give it.
local admins = require(game.ServerScriptService.Admins)
for admins, admin in ipairs(admins) do
local player = game.Players:WaitForChild(admin)
if player then print("FOUND", player) else print("NOOO") end
local gun = game.ReplicatedStorage.Gun:Clone()
print(gun.Parent)
gun.Parent = player.Backpack
print(gun.Parent)
end
it even says that the parent is the backpack
but nothing appears (the torch is irrelevant)
2 things, there is no need for ipairs, just use pairs, and the first variable that you named admins should be i for index
If I was doing this, I would just do
for _,Player in pairs(game.Players:GetChildren()) do
if table.find(admins, Player) ~= -1 then
local Gun = game.ReplicatedStorage.Gun:Clone()
Gun.Parent = Player.Backpack
end
end
If this script is a server script and not a local script, this code should work. Is your script a local script?
I cleaned the script, which could be potentially another issue:
local admins = require(game.ServerScriptService.Admins)
for admins, admin in ipairs(admins) do
local player = game.Players:WaitForChild(admin)
if player then
print("FOUND", player) else print("NOOO")
end
local gun = game.ReplicatedStorage.Gun:Clone()
print(gun.Parent)
gun.Parent = player.Backpack
print(gun.Parent)
end
Your if statement and your code was all on one line. Sometimes even if you fix other problems there can still be an issue with your layout.
@Puzzled3d, you can put if on one line but not the whole resulting code. The console is also set up differently compared to a script.
local admins = require(game.ServerScriptService.Admins)
for _,Player in pairs(game.Players:GetChildren()) do
if table.find(admins, Player) ~= -1 then
local Gun = game.ReplicatedStorage.Gun:Clone()
Gun.Parent = Player.Backpack
print(Gun.Parent)
end
end
didn’t know what admins were so I kept the first line
local admins = require(game.ServerScriptService.Admins)
for _,Player in pairs(game.Players:GetChildren()) do
if table.find(admins, Player.UserId) ~= -1 then
local Gun = game.ReplicatedStorage.Gun:Clone()
Gun.Parent = Player.Backpack
print(Gun.Parent)
end
end
The problem here is simple: You’re only looping through the players once. You might think that the WaitForChild() would work but since the code inside the loop isn’t in a seperate thread, it’ll wait for the admins one by one, thus halting the code if the next player in the table isn’t in the server.
For a safer approach, use PlayerAdded then adapt your script to that:
local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local admins = require(serverScriptService.Admins)
players.PlayerAdded:Connect(function(player)
if not table.find(admins, player.UserId) then return end
local gun = replicatedStorage.Gun:Clone()
gun.Parent = player.Backpack
end)
However, this would just give the gun when they join. If they die, they lose it. If you want me to edit the script and implement that feature, let me know.
Also I suggest using the UserId when it comes to player whitelists. Let’s say the player changes their username; they wouldn’t be able to have the gun since their new username isn’t in the admins table. However, Ids do not change. Your admins module should look like this:
return {
123123123, -- you can put the admin names next to the id like this for easier reading
123456789,
690000000
}