As shown in this video, this white square can go through the right and left border and ends up teleporting back up or down a row as well.
How do i prevent this and get the square to stop when it hits the edges?
NOTE: Each cell is a frame which is counted from 1 to 100 from the top left to the bottom right corner of the canvas.
function MovePlayer()
for index, CanvasCell in ipairs(GameCanvas:GetChildren()) do
if CanvasCell:IsA("Frame") and CanvasCell.CellType.Value == "Player" and not PlayerMoved then
if CurrentPlayerDirection == "Up" then -- Move up
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved up")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Down" then -- Move down
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved down")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Right" then -- Move right
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved right")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Left" then -- Move left
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved left")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
end
end
end
RefreshCanvas()
PlayerMoved = false
end
local TableOfNotApprovedCellsLeft = {1,11,21,31,41,51,61,71,81,91}
local TableOfNotApprovedCellsRight = {10,20,30,40,50,60,70,80,90,100}
function MovePlayer()
for index, CanvasCell in ipairs(GameCanvas:GetChildren()) do
if CanvasCell:IsA("Frame") and CanvasCell.CellType.Value == "Player" and not PlayerMoved then
if CurrentPlayerDirection == "Up" then -- Move up
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved up")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Down" then -- Move down
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved down")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Right" then -- Move right
if not table.find(TableOfNotApprovedCellsLeft,CanvasCell.LayoutOrder + 1) then
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved right")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
end
elseif CurrentPlayerDirection == "Left" then -- Move left
if not table.find(TableOfNotApprovedCellsRight,CanvasCell.LayoutOrder - 1) then
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved left")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
end
end
end
end
RefreshCanvas()
PlayerMoved = false
end
This works, but using large tables as a list of unapproved cells to move to isn’t the best due to customizable resolution. But i can have some code that automatically fills them depending on the amount of cells or something like that.
local CellsInOneLine = 10
local AllCellsTogether = 100
local TableOfNotApprovedCellsLeft = {}
local TableOfNotApprovedCellsRight = {}
local CountLeft = 1
for i=1,CellsInOneLine do
table.insert(TableOfNotApprovedCellsLeft,CountLeft)
CountLeft = CountLeft + CellsInOneLine
end
local CountRight = 0
for i=1,CellsInOneLine do
CountRight = CountRight + CellsInOneLine
table.insert(TableOfNotApprovedCellsLeft,CountLeft)
end