Assign values to players from a table?

This might not make much sense, but I’ll try my best to convey what I mean

I want to give each player a value (1 - 10) when they join, this is for a tycoon type game, where the number corresponds to their plot. I am struggling both with how to assign these values, and how to make them available again after a player with that number leaves. Any ideas on how I can achieve this? Thanks :slight_smile:

Do you want them ordered from 1 to 10 or random?

Ordered 1 - 10 would be nice, but either would work I suppose :+1:

Alright then, Lets get started!

For this, we wouldn’t neccisarily need a table, we can just assign the value directly to the player.

Step 1 Detect when the player joins, keep track of the open slots, and assigning them their slot number

local slot1 = nil
local slot2 = nil
local slot3 = nil
local slot4 = nil
local slot5 = nil
local slot6 = nil
local slot7 = nil
local slot8 = nil
local slot9 = nil
local slot10 = nil

game.Players.PlayerAdded:Connect(function(plr)
     if slot1 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 1
           slot1 = plr
    elseif slot2 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 2
           slot2 = plr
    elseif slot3 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 3
           slot3 = plr
    elseif slot4 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 4
           slot4 = plr
    elseif slot5 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 5
           slot5 = plr
    elseif slot6 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 6
           slot6 = plr
    elseif slot7 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 7
           slot7= plr

    elseif slot8 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 8
           slot8= plr
    elseif slot9 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 9
           slot9= plr
    elseif slot10 == nil then
           local value = Instance.new('NumberValue', plr)
           value.Value = 10
           slot10= plr
     end
end

Step 2 Check if a player leaves and make them available again

game.Players.PlayerRemoving:Connect(function(plr)
    if plr:FindFirstChild('Value') then
        if slot1 = plr then
            slot1 = nil
        elseif slot2 = plr then
            slot2 = nil
        elseif slot3 = plr then
            slot3 = nil
       elseif slot4 = plr then
            slot4 = nil
       elseif slot5 = plr then
            slot5 = nil
       elseif slot6 = plr then
            slot6 = nil
       elseif slot7 = plr then
            slot7 = nil
       elseif slot8 = plr then
            slot8 = nil
       elseif slot9 = plr then
            slot9 = nil
       elseif slot10 = plr then
            slot10 = nil
       end
    end
end

Try this out and tell me what happened

Just ran it in a test server with 2 clients and step 1 worked perfectly :slight_smile:
Not sure how I’ll be able to test step 2 though.
Thanks so much for this.

1 Like

You can test step 2 by leaving the game on one of the 2 clients and seeing if what you want happens. Or you can use the roblox player and the roblox app to test 2 different accounts.

Put a print statement after “slot2 = nil” and left of the second client. The print statement ran so I think we’re all good :sunglasses: cheers boss

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.