Auto claim plot on join not working

Hi,

I’m working on a tycoon where if you join the game you’re auto assigned to one of the available plots. When the plot is claimed, the player’s username will show on a sign outside.

The name isn’t updating on the text label for some reason. I’d appreciate help on what I’m doing wrong :slight_smile:

This is the localscript in serverscriptservice:

local Players = game:GetService("Players")
local PlotsFolder = workspace:WaitForChild("Plots")

local MAX_PLOTS = 6
local assignedPlots = {}

local function getAvailablePlot()
	for _, plot in ipairs(PlotsFolder:GetChildren()) do
		if not assignedPlots[plot] then
			return plot
		end
	end
	return nil
end

local function assignPlotToPlayer(player)
	local plot = getAvailablePlot()
	if plot then
		assignedPlots[plot] = player
		player:SetAttribute("AssignedPlot", plot.Name)

		local spawnLocation = plot:FindFirstChild("SpawnLocation")
		if spawnLocation then
			player.CharacterAdded:Connect(function(char)
				char:WaitForChild("HumanoidRootPart").CFrame = spawnLocation.CFrame + Vector3.new(0, 3, 0)
			end)
		end

		local signModel = plot:FindFirstChild("Sign")
		if signModel then
			local frontPart = signModel:FindFirstChild("Front")
			if frontPart then
				local surfaceGui = frontPart:FindFirstChildWhichIsA("SurfaceGui")
				if surfaceGui then
					local textLabel = surfaceGui:FindFirstChildWhichIsA("TextLabel")
					if textLabel then
						textLabel.Text = player.Name
					end
				end
			end
		end
	end
end

local function freePlot(player)
	for plot, assignedPlayer in pairs(assignedPlots) do
		if assignedPlayer == player then
			assignedPlots[plot] = nil

			local signModel = plot:FindFirstChild("Sign")
			if signModel then
				local frontPart = signModel:FindFirstChild("Front")
				if frontPart then
					local surfaceGui = frontPart:FindFirstChildWhichIsA("SurfaceGui")
					if surfaceGui then
						local textLabel = surfaceGui:FindFirstChildWhichIsA("TextLabel")
						if textLabel then
							textLabel.Text = "Available"
						end
					end
				end
			end
		end
	end
end

Players.PlayerAdded:Connect(assignPlotToPlayer)
Players.PlayerRemoving:Connect(freePlot)

Explorer:

1 Like

I was confused until I saw it was a localscript? in serverscriptstorage??? ???

4 Likes

Certainly; additionally, I think making it a server script is better in this case

1 Like

obv

1 Like

Can’t believe I didn’t realize that. Thanks!

1 Like

fr, u so good but made a little uncorrect thing, keep going!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.