Anyone have any idea how to accomplish something like Welcome to Bloxburg’s property system, spawning into the game and being able to choose your plot ( depending if one is taken or not?)
I’ve tried to use math.random but it doesn’t seem to be much helpful.
Simply give each plot a BoolValue and have a script change it to true or false once a plot is taken. Remember to add a BoolValue.Changed in-case the player is about to choose the plot and someone else already picked it.
There is multiple ways to go about this codewise unfortunately so you’ll have to actually try making it.
BoolValues, tables, etc
Posted this a while ago for someone else. This will need a lot of altering for you to actually get it implemented though, but if anything, it can be your pseudo code starting point:
local Module = { }
-- Env
local Plots = game.Workspace.Plots
-- Local
local function create(obj)
obj = Instance.new(obj)
return function (props)
for prop, val in next, props do
obj[prop] = val
end
return obj
end
end
local function randomPlot()
local curPlots = Plots:GetChildren()
local Plot
repeat -- we can perform this without killing our thread, assuming that you do have enough plots within the game for each player
Plot = curPlots[math.random(1, #curPlots)]
until not Plot:FindFirstChildOfClass 'StringValue'
return Plot
end
function Module.new(StorageManager, Events)
local this = { }
this.update = Instance.new "BindableEvent"
this.states = {
plots = { }
}
function this:assignPlot(Player, Plot)
if Plot then
-- Check if player has gamepass to select their own plot
-- Double check client isn't lying to us
Plot = Plots:FindFirstChild(Plot)
if Plot and Plot:FindFirstChildOfClass 'StringValue' then
Plot = nil
end
end
Plot = Plot or randomPlot()
-- Assign to player
create 'StringValue' {
Name = "Owner";
Value = Player.Name;
Parent = Plot;
}
this.states.plots[Player.Name] = Plot
-- Build plot
spawn(function ()
this:buildPlot(Player)
--> Don't forget you need to pass the current house data
Events.toClients:FireClient(Player, "PlotAssignment", time(), {
Plot = Plot;
})
end)
end
function this:getPlot(Player)
local Plot = this.states.plots[Player.Name]
if Plot then
local Owner = Plot:FindFirstChildOfClass 'StringValue'
if Owner and Owner.Value == Player.Name then
return Plot
end
end
end
function this:buildPlot(Player)
local Plot = this.states.plots[Player.Name]
if Plot then
local Owner = Plot:FindFirstChildOfClass 'StringValue'
if Owner and Owner.Value == Player.Name then
-- Build plot based on StorageManager
end
end
end
function this:clearPlot(Player, Plot)
local Plot = Plot or this.states.plots[Player.Name]
if Plot then
local Owner = Plot:FindFirstChildOfClass 'StringValue'
if Owner and Owner.Value == Player.Name then
for _, v in Plot:GetChildren() do
if v ~= Plot.PrimaryPart then
v:Destroy()
end
end
end
end
end
function this:unassignPlot(Player)
local Plot = this.states.plots[Player.Name]
if Plot then
local Owner = Plot:FindFirstChildOfClass 'StringValue'
if Owner and Owner.Value == Player.Name then
this:clearPlot(Player, Plot)
this.states.plots[Player.Name] = nil
end
end
end
return this
end
return Module
Would you know any way to grab a list of plots that aren’t occupied? I can’t figure out any way to do it other than loops, however I’m doing a mousebutton1click-plot choose system, and my script is a normal script, so I’m not sure what to do there.
Thanks for the help
Do you mean you need to determine this from a localscript so the interface shows which plots are available? If so, the easiest way to do it is loops as you mentioned. Other than that, you could hook up a RemoteFunction to the module I gave you above, and have a function that returns unused plots (assuming you track which is occupied in a table).
If you’re doing it on a server-side script, either loop once again, or if that script has access to that instance of the module above you could have a public function which returns the tracked list of plots.
I’ll give it a shot. Thanks for the help.