Hello, I’m currently trying to code a plot system, but I’m having trouble with my script. I’m following a tutorial, and the part I’m having trouble with is displayed here.
This is my script so far (not the finished script, but the same as the tutorial at the point i’m at from what I can see, but does not replicate what his does.) When I click my left/right arrows, there’s nothing in the “SelectedPlot” value, no count up or down like Alvin has in his video if you look in his properties section. Does anyone know whats going wrong?
local Player = game.Players.LocalPlayer
local PlotSelect = script.Parent:WaitForChild("PlotSelect")
local Frame = PlotSelect:WaitForChild("Frame")
local Left = Frame:WaitForChild("Left")
local Right = Frame:WaitForChild("Right")
local SelectedPlot = Frame:WaitForChild("SelectedPlot")
local plots = game.Workspace.Plots
local function findUnoccupiedPlots()
local availablePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availablePlots,plot)
end
end
return availablePlots
end
local plots = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = plots[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index -= 1
else
index = 9
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index += 1
else
index = 1
end
end)
local Player = game.Players.LocalPlayer
local PlotSelect = script.Parent:WaitForChild("PlotSelect")
local Frame = PlotSelect:WaitForChild("Frame")
local Left = Frame:WaitForChild("Left")
local Right = Frame:WaitForChild("Right")
local SelectedPlot = Frame:WaitForChild("SelectedPlot")
local Plots = game.Workspace.Plots
local function findUnoccupiedPlots()
local availablePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availablePlots,plot)
end
end
return availablePlots
end
local plotsTable = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = Plots[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index -= 1
else
index = 9
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index += 1
else
index = 1
end
end)
Weird… Make sure you are looking at the properties of the player (not starter GUI) and the code only changes the property when you press the right button. (you haven’t included that line in the left function, yet) Also it seems unintuitive why does right decrease the value?
Okay, so, I looked inside of the player properties instead and the value did appear, and I changed the left/right around for the increase/decrease value, and the output now says this:
17:41:05.887 Players.xshaybie.PlayerGui.Client:35: attempt to index nil with number - Client - Client:35
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index -= 1
else
index = 9
end
SelectedPlot.Value = plotsTable[index]
end)
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index += 1
else
index = 1
end
SelectedPlot.Value = plotsTable[index]
end)
local Player = game.Players.LocalPlayer
local PlotSelect = script.Parent:WaitForChild("PlotSelect")
local Frame = PlotSelect:WaitForChild("Frame")
local Left = Frame:WaitForChild("Left")
local Right = Frame:WaitForChild("Right")
local SelectedPlot = Frame:WaitForChild("SelectedPlot")
local Plots = game.Workspace.Plots
local function findUnoccupiedPlots()
local availablePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availablePlots,plot)
end
end
return availablePlots
end
local plotsTable = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = Plots[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index -= 1
else
index = 9
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index += 1
else
index = 1
end
end)
can you try printing the availablePlots variable? By putting this line: print(plotsTable) above the line that is erroring and see what the output says?
this worked, only slightly though. when i scroll through the numbers it counts randomly through the plots instead of in order, and the left button doesn’t work