Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help With Placing Houses in a Neighborhood For Players?

Okay, so heres my problem. This may be a bit confusing but I hope someone can understand what I am saying so that they can help me with this.

I want to be able to spawn houses into a neighborhood when players enter the game. But the problem is that when I spawn them they need to spawn to a specific location. So when player 1 spawn House spawns in player 1's set location. The same goes for player 2,3,4.. The problem with this is that if for instance 4 players spawn, of course 4 houses would spawn in their set locations.

House 1 spawns at House 1 location. House 2 spawns at House 2 location. House 3 spawns at House 3 location. House 4 spawns at House 4 location.

HERE IS THE PROBLEM. If player 2 leaves and another player enters How do I fill player 2's location with the next player that enters? If I do a counting script the house would spawn at player 3's location and I would have a house on top of a house.

I basically need to be able to have players come in and if for instance player 2 leaves then the next player to join the server, his/her house would spawn in the given location where player 2's house used to be.

I know this is a tough question but if someone could just give me some advice on where to start I would be very grateful. I have tried numerous things but they did not work. Thanks in advance.

1 answer

You could have a table for every available plot. Then every time a player joins, remove the plot from the available plots. This would look like this:

local AvailablePlots = {"House1", "House2", "House3", "House4"}
local TakenPlots = {}

game.Players.PlayerAdded:Connect(function(Plr)
	local HouseToClone = game.ServerStorage.HouseToClone:Clone() -- Assuming you're cloning a house
	--Identify where to put the house
	if AvailablePlots[1] = "House1" then
		HouseToClone.Parent = game.Workspace
		HouseToClone:MoveTo(Vector3.new("?")) -- Where is house1 supposed to move to
		table.insert(TakenPlots, AvailablePlots[1]) -- Adds the plot to taken so that when the players leaves we can add the house back to avaible
		table.remove(AvailablePlots, 1) -- Removes the plot so no 2 houses are on top of each other
	elseif AvailablePlots[1] = "House2" then
		HouseToClone.Parent = game.Workspace
		HouseToClone:MoveTo(Vector3.new("?")) -- Where is house2 supposed to move to
		table.remove(AvailablePlots, 1)	
--etc, etc until you have a situation for every plot in your game.
	end
end)

Now we need to adjust for when the player leaves

game.Players.PlayerRemoving:connect(function(Plr)
	-- Destroy the house of the player that left however you've been doing it
	table.insert(AvailablePlots, --[[The plot that the player left]] ) --
	for I, V in ipairs(TakenPlots) do
		if V == --[[The plot that the player left]]  then
			table.remove(TakenPlots, I) -- Makes the blank plot now avaible
		end
	end 
end)

4 comments

0

Dang... I was thinking of using a table to store it... I swear I have been down a rabbit hole with this all day.. THANK U

0

OK So 1 question How am I able to find the plot that the player left behind? Under removing player Above I see thatyou have a tabe.insert (AvailablePlots) But how do we find the value for the plot that the said player left behind? This made the other script that I wrote for this function much easier and a lot simpler but this is the same problem I was running into before. That when someone leaves

0

If it were me, I would probably keep a StringValue inside each house model. Then when the player joins you can set the string value in the house to the players name. Then, Every time a player leaves you can check each house and see if the value is equal to the player that left name. To get the players name, the (game.Players.PlayerRemoving:connect(function(Plr) that I mentioned in my answer just

0

o get the players name, the (game.Players.PlayerRemoving:connect(function(Plr) that I mentioned in my answer just do Plr.Name. The line of code should look something like if House.StringValue.Value = Plr.Name then House:Destroy()

This discussion is preserved as it was. New answers, comments, and votes are closed.