How would I finish this script?

So im trying to make a script so where when a player joins they automatically get assigned a random area ingame, the problem is I don’t know how i’d assign it to a player or stop the script from picking an area that’s already assigned to someone.

script


game.Players.PlayerAdded:WaitForChild("Humanoid")
	local rand = math.random(1,#cub)
	local cube = cub[rand]```

You should grab the character, and then :WaitForChild(“Humanoid”)

Yikes you’re right, I only added that part right before I posted so it would make sense. Thank you!

1 Like

PlayerAdded is an event. Connect a function to this like :

  local players = game:GetService("Players")

  players.PlayerAdded:Connect(function(player)
         local char = player.Character or player.CharacterAdded:Wait()
         -- do stuff
  end)

-- or

   players.PlayerAdded:Connect(function(player)
       player.CharacterAdded:Connect(function(char)

       end)
   end)

or you could do it this way :

local function OnPlayerAdded(player)  
      print(player.Name)
      -- get the character
end

players.PlayerAdded:Connect(OnPlayerAdded)

Also you can get a random instance from an array of instances like this:

  
    local areas = { 
    
    workspace.Part;
    workspace.location;
    workspace.area1; 
    workspace.area2;
 
                }
 
    print("random instance = ".. areas[math.random(1, #areas)].Name)

1 Like

Still need to figure out how to stop it from picking the same area again after a player has already gotten that area.

One way to do that is to add a bool value into each of the areas named “available” (or whatever you want it to be) and set it to true. When the player gets an area selected, then set that area’s available value to false. Make sure to add an

if area[something].available == true 

when assigning the area to the player.

Haha, I was just about to do that
I was thinking and it just came to me
Thank you though!