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

?Assigning Player1, Player2, Player3 etc to 4 players in a game?

Asked by
kag_e 12
5 years ago

Essentially when a Player is added to the game I want that Player to be assigned either P1, P2, P3 or P4 based on which ones are already taken. I have attempted this in several ways even though I could only think of a few. I will include an example of my code (doesn't work but will most likely help you understand my mindset of this)

local P1 = game.ReplicatedStorage:WaitForChild("Values").P1
local P2 = game.ReplicatedStorage:WaitForChild("Values").P2
local P3 = game.ReplicatedStorage:WaitForChild("Values").P3
local P4 = game.ReplicatedStorage:WaitForChild("Values").P4
local Values = game.ReplicatedStorage:WaitForChild("Values")

game.Players.PlayerAdded:Connect(function(player)
    local imAssigned = false
    if P1.Assigned.Value == false and imAssigned == false then
        P1.Value = player.Name
        P1.Assigned.Value = true
        imAssigned = true
    end
    if P2.Assigned.Value == false and imAssigned == false then
        P2.Value = player.Name
        P2.Assigned.Value = true
        imAssigned = true
    end
    if P3.Assigned.Value == false and imAssigned == false then
        P3.Value = player.Name
        P3.Assigned.Value = true
        imAssigned = true
    end
    if P4.Assigned.Value == false and imAssigned == false then
        P4.Value = player.Name
        P4.Assigned.Value = true
        imAssigned = true
    end
end)

Basically there are 4 string values and I want when a new player joins for their name to be set as the value of that string value. But only 1..

So another way of explaining this is: Player Joins > Game Sets ONE of the values that is "none" or nil to that Players name and sets the value.Assigned (Boolean Value) to true.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I'm assuming the following

  1. game.ReplicatedStorage:WaitForChild("Values") is a folder
  2. Everything ins the object "Values" are string values and that is it. If you have other objects in the folder which are not stringvalues, you need to move them somewhere.
  3. This is in a server script in workspace or serverscriptservice.
  4. You are only letting 4 players to be in the game.
  5. The "Assigned" object from P1 to P4 are bool values
-- You are over complicating this area of the script, you don't need to write "game.ReplicatedStorage:WaitForChild("Values")" for each one. Also, using tables, writing down the variables from P1 to P4 is useless.
local Values = game.ReplicatedStorage:WaitForChild("Values")
--[[local P1 = Values.P1
local P2 = Values.P2
local P3 = Values.P3
local P4 = Values.P4]]

playerlist = Values:GetChildren() -- This makes a table for each item in the values folder

game.Players.PlayerAdded:Connect(function(player)
    --I'm confused as to what "imAssigned" is suppose to mean, so I'm getting rid of it.
    --local imAssigned = false

    --Also, you don't need to script for each object p1-p4, you just need to use a "for" loop
    for i,playertype in pairs(playerlist) do
        if playertype.Assigned.Value == false then -- This condition checks to see if this value is true. Meaning that the player is assigned to the first unassigned position
            --P4.Value = player.Name
            -- ^You have this part of the script backwards, you need to flip both sides, additionally, changing the players name like that is not a good idea. I think it is better if you do something like changing the name of the character. In this context character means the model the player controls. But for now we will change the name property of the player
            player.Name = playertype.Value
            playertype.Assigned.Value = true
        end
    end
end
-- In addition to the script above, you might want to add in an event whenever a player leaves
game.Players.PlayerRemoving:connect(function(player)
    for i,v in pairs(playerlist) do
        if v.Value == player.Name Then
            v.Assigned.Value = false
        end
    end
end

Cleaner Script

local Values = game.ReplicatedStorage:WaitForChild("Values")

playerlist = Values:GetChildren() 
game.Players.PlayerAdded:Connect(function(player)
    for i,playertype in pairs(playerlist) do
        if playertype.Assigned.Value == false then 
            player.Name = playertype.Value
            playertype.Assigned.Value = true
        end
    end
end
game.Players.PlayerRemoving:connect(function(player)
    for i,v in pairs(playerlist) do
        if v.Value == player.Name Then
            v.Assigned.Value = false
        end
    end
end

Tags: For Loops, Tables, UserDataValues, Players, ReplicatedStorage, Character

0
Hi, I tried out the script but it sets ALL the values in Values to the players name, I don't want the players name constantly repeating kag_e 12 — 5y
Ad

Answer this question