local user = --id local tool = game.ServerStorage.GreenBalloon function onPlayerSpawned(player) tool:Clone().Parent = player.Backpack end game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function() onPlayerSpawned(player) end) end)
This script gives my friend a gear when he joins, but I want this to be able to apply to more players. Any idea where I should start?
You'd use tables!
A table is a data type in Lua that is useful to store multiple values. Each value in the table is separated by a comma and is indexed 1 through the number of items in the table.
local users = {123432, 525322, 32523523} --Assume these are userIds. Notice they are separated by a comma!
We can use a For loop to iterate through each item in that table and see if any value in the table matches with the player who joined's UserId.
The for loop will run i times (i = amount of items in the table)
local function checkIfUserIsAdmin(userId) for i, v in pairs(users) do --i = index, v = value. These are variables, and as such can be named whatever you want if userId == v then --If the player who joined's UserId is the same as the value we're checking, return true return true else return false end end end
To help you understand for loops more:
local users = {123, 312, 542} for i, v in pairs(users) do --Iterate through the users table. print(i, v) end
The expected output would be the following(Note that the loop ran three times where i= the number of times the loop ran and v = the value):
1 123
2 312
3 542
As such, your code can be re-written as:
local users = {123432, 525322, 32523523} local tool = game.ServerStorage.GreenBalloon local function checkIfUserIsAdmin(userId) for i, v in pairs(users) do --i = index, v = value. These are variables, and as such can be named whatever you want if userId == v then --If the player who joined's UserId is the same as the value we're checking, return true return true else return false end end end function onPlayerSpawned(player) if checkIfUserIsAdmin(player.UserId) then -- The function returned true tool:Clone().Parent = player.Backpack else --The function returned false print("User does not have permission") end end game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function() onPlayerSpawned(player) end) end)
You'd make a table of users for it to work and loop through the table so:
local users = {"Player1", "Player2", "Player3"} -- Change these values to whatever your player names are game.Players.PlayerAdded:Connect(function(player) for i,v in pairs(users) do -- loops through the table if v == player.Name then -- checks if the player who joined is in the table local clone = game.ServerStorage.GreenBalloon:Clone() -- they are so give them a balloon clone.Parent = player.Backpack end end end)