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

How to allow multiple UserIds to give a tool to those players?

I am currently working on a script that will allow multiple UserIDs to get one tool. I do NOT want to use an or statement with the IDs.

Here is what I have so far:

local allowedID = {
    955684786, --Supersuccdude56
    1, --ROBLOX
}
local tools = {script.Name}; --Name of the tools in ServerStorage go here
local SS = game:WaitForChild("ServerStorage")

--DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING--
game.Players.PlayerAdded:connect(function(player)
    repeat wait(0.1) until player.Backpack    
    repeat wait(0.1) until player.StarterGear
    if allowedID[player.UserId] then
    for i = 1,#tools do
    SS:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
    SS:FindFirstChild(tools[i]):Clone().Parent = player.StarterGear
end
end
end)

1 comment

0

That should work

1 answer

This answer is entirely based on what I assume is happening. If I'm incorrect on anything, please correct me in the comments!

On line 12, you're trying to check whether a player is in a table by doing allowedID[player.UserId]. Doing that doesn't actually get the value itself, but what it's assigned to, if that makes sense.

Here's a rough visual of what I mean:

--What the table looks like to you

local allowedID = {
	955684786,
	1,
}

and

--What the table actually is

local allowedID = {
	[955684786] = nil,
	[1] = nil,
}

So you're actually getting nil for every value, which is why your script isn't working the way you want it to. Doing table[valuethatisnotanumber] is really more for dictionaries, which are pretty much tables but with variables inside them.

If you want to check if something is directly in a table, you can use table.find().

Here's how you would apply it to your script:

local allowedID = {
    955684786, --Supersuccdude56
    1, --ROBLOX
}
local tools = {script.Name}; --Name of the tools in ServerStorage go here
local SS = game:GetService("ServerStorage") -- Use :GetService() for services, not :WaitForChild()

--DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING--
game.Players.PlayerAdded:connect(function(player)
    repeat wait(0.1) until player.Backpack    
    repeat wait(0.1) until player.StarterGear
    if table.find(allowedID, player.UserId) then -- table.find(table, valuetofind) returns the value in the table if found, otherwise returns nil.
	    for i = 1,#tools do
		    SS:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
		    SS:FindFirstChild(tools[i]):Clone().Parent = player.StarterGear
		end
	end
end)

You can learn more about tables and dictionaries here.

You can learn more about table functions here.

Sorry for over-complicating this. If you have any questions, just comment!

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