I am trying to create a Chess Engine and I want to make a Pawn move forward by pressing “W”. However, it does not work and I get the error shown in the title. For more info, pressing “WASD” can change where the pawn moves, but it doesn’t secure the position, pressing space will secure the position (so can’t be moved) I haven’t implemented ASD and space because W doesn’t work.
function Pawn:Move(currentPos, validPosTable)
for i, v in pairs(validPosTable) do
validPosTable[i] = Engine:GetCoord(v)
end
UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.W then
for i, v in pairs(validPosTable) do
if Engine:GetCoord(currentPos)[1] == v[1] and v[2] - Engine:GetCoord(currentPos)[1] == 1 then
local nextCoord = {Engine:GetCoord(currentPos)[1], v[2]}
local nextAlgebraic = Engine:GetAlgebraic(nextCoord)
local pawnPiece = Chessboard[currentPos]:FindFirstChildWhichIsA("ImageLabel")
pawnPiece.Parent = Chessboard[nextAlgebraic] --error occurs
end
end
end
end)
end
for _, frame in pairs(script.Parent.Chessboard.Frame.Board:GetChildren()) do
for _, button in pairs(frame:GetChildren()) do
if button:IsA("TextButton") then
button.Activated:Connect(function()
--reset square to default color
for i, v in pairs(defaultColors) do
Chessboard[i].BackgroundColor3 = v
end
--check to make sure pawn is in square
if #button.Parent:GetChildren() > 1 and string.match(button.Parent:FindFirstChildOfClass("ImageLabel").Name, "wPawn_") then
frame.BackgroundColor3 = Color3.fromRGB(150, 189, 147)
PawnEngine:Move(button.Name, PawnEngine:GetValidMoves(button.Name))
for _, v in pairs(PawnEngine:GetValidMoves(button.Name)) do
Chessboard[v].BackgroundColor3 = Color3.fromRGB(202, 255, 198)
end
end
--continue to move image
button.Parent:FindFirstChildOfClass("ImageLabel").AncestryChanged:Connect(function(_, parent)
PawnEngine:MoveTo(parent.Name, PawnEngine:GetValidMoves(parent.Name))
end)
end)
end
end
end
This might seem confusing but here is what the code is trying to do:
Chessboard
|
|__ Square tile (A1, B2, C3 chess coord etc.)
| |
| |__ Button ( for selecting)
| |
| |__ Image1 ( actually displays the pawn)
|
|__ Another Square tile
|
|__ Image1 does not move here it should
I know its confusing because I have like 10 other scripts not here but if anyone needs more clarification, please ask!