Making a variable for the plot of a player

Hello,
This is my first post on the devforum.

So, I’ve been working on a placement system, and I wanted to add an important feature. Saving the building the player has built.

I’ve successfully managed to create what I wanted before, but after adding the plot system, things have changed.

My plot system can only be defined by an object value on the replicated storage, which handles the plot that the player had got.

The problem is, when I try to make a variable about it in a server script, it doesn’t work. When making a variable about it in a client script. It works.

My plot object value:

My plot handler (Server Script Service):

game.Players.PlayerAdded:Connect(function(plr)
	for _, plt in pairs(workspace.Plots:GetChildren()) do 
		
		if plt.Owner.Value ~= nil then
			
		else
			plt.Owner.Value = plr  -- Object Value
			
			plt.OwnerName.Value = plr.Name
			
			game.ReplicatedStorage.PlotName.Value = plt
			
			break
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	for _, plt in pairs(workspace.Plots:GetChildren()) do
		if plt.Owner.Value ~= nil then
			
			plt.Owner.Value = nil
		else
			
		end
	end
end)

My plot local script (Starter GUI):

game.ReplicatedStorage.Events.GetPlot.OnClientEvent:Connect(function(plr, plt)
	game.ReplicatedStorage.PlotName.Value = plt
end)

When joining tha game, a random plot wil be choosen to be my plot.
image
And the object value will be set to my plot.
image

Any help?
Thank you.

What the client changes in ReplicatedStorage cannot be seen by the Server, try using Remote Events.

1 Like

So I can fire a remote event on a cleint server and return it to the server script?

Hi,
follow this rule, “Never trust the client”

But what is : “plt.OwnerName.Value = plr.Name” ?
I see plt.Owner.Value but not OwnerName.Value

Are you sure you have writed the good variable name ?

1 Like

Not neccesary return, but just FireClient back to the client.

1 Like

OwnerName is just a string value on the plot, it returns the player’s name. Created this to make a mailbox system. And yes, I did write it correctly.

Use an ObjectValue instance instead which refers/points to the plot itself (assuming the plot is a model instance).

What do you mean?
You mean I’ll create a new object value using Instance.new?

local players = game:GetService("Players")
local plotsFolder = workspace.Plots
local plots = plotsFolder:GetChildren()

players.PlayerAdded:Connect(function(player)
	local plotValue = Instance.new("ObjectValue")
	plotValue.Name = "Plot"
	plotValue.Parent = player
	
	local otherPlayers = players:GetPlayers()
	for _, plot in ipairs(plots) do
		for index, otherPlayer in ipairs(otherPlayers) do
			if otherPlayer ~= player then
				if otherPlayer.Plot then
					if otherPlayer.Plot.Value then
						if otherPlayer.Plot.Value == plot then
							break
						end
					end
				end
			end
			if index == #otherPlayers - 1 then
				plotValue.Value = plot
			end
		end
	end
end)

This might look a little confusing at first.

1 Like

Ah, I understand.
So, I have to make the object inside of the player.

Gonna try that.

After some time, I managed to make it!

Thank you all for your support by the way :smiley: