I’ve been spending the last two hours figuring out how to give players items based on their team or their user ID. I don’t know why, but any guide I try to find online is outdated or irrelevant. I cannot figure out how to solve this problem.
Here’s the script I currently have. I’ve realized LocalPlayer doesn’t work in a server script, but I can’t figure out what alternative I should use. I also would like to use an array instead of elseif but I don’t know how to do that. I’m also unsure how I could make it team dependent, but I haven’t started looking into that yet.
local Card = game.ServerStorage.Card
local Clone = Card:Clone()
game.Players.PlayerAdded:Connect(function(Player)
if Player.UserId == 1120891133 then
Clone.Parent = game.Players.LocalPlayer.Backpack
elseif Player.UserId == 2239520047 then
Clone.Parent = game.Players.LocalPlayer.Backpack
end
end)
Great start to your script, but as you said, an array will work better in terms of scaling.
A few issues with your script:
You are only cloning the card once, so that cloned card (assuming it doesn’t disappear from dying, for example), is just being tossed around players.
The Backpack of the Player is the container of their hotbar tools. A Player respawning will wipe all those tools with new ones from the StarterPack and the Player’s StarterGear. Because of this, when the Player joins the server and something gets added to their Backpack immediately after using PlayerAdded, if their Character have not yet spawned, the Backpack will wipe it’s contents upon spawning. Because of this behavior, you want to clone the card after they spawn.
With these in mind, let’s rewrite the intent of your original script with a few good practices:
-- it's good practice to variablize all your services using
-- GetService (which matters for some services)
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
-- Variables for the Card and UserIds
local Card = ServerStorage.Card
local UserIds = {995421704, 1120891133, 2239520047}
-- PlayerAdded function
local function onPlayerAdded(player)
-- CharacterAdded function
local function onCharacterAdded(character)
-- We use the table.find function to see if the player's UserId is
-- in the list. It will return the index position if it exists, and
-- nil if it does not.
if table.find(UserIds, player.UserId) ~= nil then
-- Clone the card tool to the Player's Backpack
Card:Clone().Parent = player.Backpack
end
end
-- We hook the CharacterAdded event to the function AND check if there
-- is already a Character that exists to run the function. This is good
-- practice because if the Character already exists before the Signal
-- gets connected, CharacterAdded will not run for the player.
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character ~= nil then
onCharacterAdded(onCharacterAdded)
end
end
-- We hook the PlayerAdded event to the function AND loop through
-- all existing players and run the function individually. This is good
-- practice because if there are already players before the Signal
-- gets connected, PlayerAdded will not run for those players.
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in Players:GetPlayers() do
-- task.spawn essentially just lets us run code in a different thread,
-- meaning this will not halt and run onPlayerAdded for every player
-- at the same time
task.spawn(onPlayerAdded, player)
end
As for team dependent, I think it would be great practice for you to figure it out! To point you in the right direction, here are the APIs you should be using: