Doesnt place tool in backpack

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

image
it even says that the parent is the backpack
image
but nothing appears (the torch is irrelevant)

image
the gun in rs

1 Like

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.

All you did was add few line breaks :skull:
If statements can be put on only one line, and the console shows that

It didn’t work. But is that even the issue?
I did

print(gun.Parent.Parent)

which came to me, but no gun
image

1 Like

It probably is, but is your Gun that you are referencing a tool?

Have you tried the code I gave above?

I did try it, but it did nothing and the gun I am referencing is a tool.

Can you show me the code please?

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

Can you show me the admins module script? I think I might have a fix

just the basic module script

local admins = {"jonaskos"}

return admins

Make it so it’s UserIds and use this code:


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

didn’t work and it doesn’t even print the print(Gun.Parent)

local admins = {"73885390"}

return admins

When you say

Are you trying to say when the torch is equipped it doesn’t appear in the players hand or what?

I was just clarifying to ignore the torch

1 Like

Are you able to show us what game.ServerScriptService.Admins contains?

image

1 Like

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
}
3 Likes

I thought this may be a problem but if you look in the output screenshot, the player is found.