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]```
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)
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