Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i rewrite this script to apply to more than one player?

Asked by 5 years ago
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?

0
I belive you should use a table. User#22722 20 — 5y
0
you could just put the balloon in starterpack francaiscanada10 33 — 5y

2 answers

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
5 years ago
Edited 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

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)

Answer this question