Hello, so the issue in my code is that the index value is correct and goes up and down like excepted but the selected plot value randomizes.
Note: There is no error printed in the console
Code:
Hello, so the issue in my code is that the index value is correct and goes up and down like excepted but the selected plot value randomizes.
Note: There is no error printed in the console
Code:
Can you copy and paste the code instead of a screenshot?
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 = workspace.Plots
local function findUnoccupiedPlots ()
local availiblePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availiblePlots, plot)
end
end
return availiblePlots
end
local plotsTable = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = plotsTable[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index = index + 1
else
index = 12
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index = index - 1
else
index = 1
end
SelectedPlot.Value = plotsTable[index]
end)
wait it randomizes when? When you make MouseButton1Click?
yes, it randomizes on MouseButton1Click
you did “Plot”…index+1) then
index = index +1
else
index = 12 maybe the issue is here
oh ok, can you send me a suggestion or a fix?
There’s no randomising.
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 = workspace:WaitForChild("Plots")
local function findUnoccupiedPlots ()
local availiblePlots = {}
for _, plot in ipairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availiblePlots, plot)
end
end
return availiblePlots
end
local plotsTable = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = plotsTable[1]
Right.MouseButton1Click:Connect(function()
local rand = math.random(1, 12)
if Plots:FindFirstChild("Plot"..index+rand) then
index += rand
else
index = 1
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
local rand = math.random(1, 12)
if Plots:FindFirstChild("Plot"..index-rand) then
index -= rand
else
index = 12
end
SelectedPlot.Value = plotsTable[index]
end)
Now there is.
suggestion I am not in studio rn sorry
when the person clicks the actual value goes to a random number between 1-12
In the version I provided, yes.
index = nil bc if you do math.random and get 12 will be 13 (bc the index was 1)
Then it’ll reset back to the default of 1 or 12 depending on which button was clicked. Similarly if the last index was 6 and you get a random roll of 9 then the index is set back to 1 and another roll is performed upon the button being clicked again.
Has nothing to do with the question, but a recommendation is to put ``` at the beginning and the end of code to make it appear better
and I’ll look into the code rn
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 = workspace.Plots
local function findUnoccupiedPlots ()
local availiblePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availiblePlots, plot)
end
end
return availiblePlots
end
local plotsTable = findUnoccupiedPlots()
local index = 1
SelectedPlot.Value = plotsTable[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index = index + 1
else
index = 12
end
SelectedPlot.Value = plotsTable[index]
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index = index - 1
else
index = 1
end
SelectedPlot.Value = plotsTable[index]
end)
No edits, just the formatting fixed.
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 = workspace.Plots
local availiblePlots = {}
for i, plot in pairs(Plots:GetChildren()) do
if plot.Occupant.Value == nil then
table.insert(availiblePlots, plot)
end
end
local index = 1
SelectedPlot.Value = availiblePlots[1]
Right.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index+1) then
index += 1
else
index = 1
end
if availiblePlots[index] then
SelectedPlot.Value = availiblePlots[index]
availiblePlots[index] = nil
end
end)
Left.MouseButton1Click:Connect(function()
if Plots:FindFirstChild("Plot"..index-1) then
index -= 1
else
index = 12
end
if availiblePlots[index] then
SelectedPlot.Value = availiblePlots[index]
availiblePlots[index] = nil
end
end)
I have a theory as to why that may be - from my experience, GetChildren
goes by the order of when the object was added under X parent. Try moving the plots to a temporary folder and placing them in the original parent in order one-by-one. I know it doesn’t sound practical or fun, but that was my solution to that problem in the past. You may also wanna use ipairs
instead of pairs
since the former guarantees order when iterating through an array (GetChildren
returns an array).
Glad to’ve been of help.
thank you so much for your help!