Giving a player a plot on join

Wondering what the best way to go about applying a “plot” of land to a player on join is?

Similar to how in bee swarm sim or most tycoons, you run upto your plot, touch something and suddenly you own it. I want that but I want it to automatically find an empty plot and give it to the player when they join.

Currently plot owners are registered as a StringValue inside the plot object

1 Like

Loop through your plots and find one that isn’t occupied and assign it. Is that what you are asking? because this question is very brief and isn’t clear.

Yeh “Loop” is the right word for what im looking for. Would I do something along the lines of
A function that triggers when a player joins, somehow loops through the plots and applies an empty one

You could try something along the lines of this.
Once a character spawns, it checks if it has a piece of land occupied, if not then it’ll just go to it’s default spawn point. If it does, it’ll teleport to the land it owns. (You can remove that piece if you don’t want it.)
It loops through the plots, which you can change the path of the plot before :GetChildren() if you have it located somewhere else. If it can’t find an unoccupied slot, then it’ll keep looping. Otherwise, it will occupy the land.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	local landOccupied = nil;
	Player.CharacterAdded:Connect(function(Character)
		if (landOccupied) and (landOccupied.PrimaryPart) then
			Character:SetPrimaryPartCFrame(landOccupied:GetPrimaryPartCFrame())
		end
	end)
	repeat
		for _, m in pairs(workspace.Plots:GetChildren()) do
			if (m:IsA("Model") and not m.Occupied.Value) then
				m.Occupied.Value = Player.Name;
				landOccupied = m;
				break;
			end
		end
		wait(0.1);
	until landOccupied;
end)

[NOTE]: Make sure each plot of land is a model with a StringValue so this may function.

This looks really great, Id like to understand exactly what is happening here tho.

I don’t understand what “m” is and how the code understands it referenced.

for _, m in pairs(workspace.Plots:GetChildren()) do
			if (m:IsA("Model") and not m.Occupied.Value) then
				m.Occupied.Value = Player.Name;
				landOccupied = m;
				break;
			end
		end
		wait(0.1);
	until landOccupied;
end)

What you put into that block is a “for loop.”
What it does in this context is that it loops through a table containing the children of the parent, “Plots.”
m is a variable which is a child of the parent, “Plots.”
If m is a model and the StringValue, “Occupied”, doesn’t have a value, then it will set the Value of “Occupied” to the player’s name. Then, it sets the variable, “landOccupied”, to the variable containing the plot’s model, m.

[NOTE]: This was written quickly, so here is a version that has minor fixes.
In this version, it’ll check if the value of “Occupied” is an empty string instead of simply non-existent.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	local landOccupied = nil;
	Player.CharacterAdded:Connect(function(Character)
		if (landOccupied) and (landOccupied.PrimaryPart) then
			Character:SetPrimaryPartCFrame(landOccupied:GetPrimaryPartCFrame())
		end
	end)
	repeat
		for _, m in pairs(workspace.Plots:GetChildren()) do
			if (m:IsA("Model") and m.Occupied.Value ~= "") then
				m.Occupied.Value = Player.Name;
				landOccupied = m;
				break;
			end
		end
		wait(0.1);
	until landOccupied;
end)

Ohhh ok, thanks for taking the time to explain! so “in pairs” is telling the loop that m is workspace.Plots children?

If I want to remove the teleport would it still function like this?

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	local landOccupied = nil;
	Player.CharacterAdded:Connect(function(Character)
		if (landOccupied) and (landOccupied.PrimaryPart) then
	repeat
		for _, m in pairs(workspace.Plots:GetChildren()) do
			if (m:IsA("Model") and m.Occupied.Value ~= "") then
				m.Occupied.Value = Player.Name;
				landOccupied = m;
				break;
			end
		end
		wait(0.1);
	until landOccupied;
end)