Hi, I need help with my script. Basically, I am making a game where you swap cups to match them to a set of cups that you cant see. The problem is, when i swap cups, some really weird stuff happens. Take a look:
Here’s my code: (It might be a little hard to read)
local firstcup1 = nil
for j, h in pairs(tableclone1.Cups:GetChildren()) do
-- looking through all the cups
h.Click.MouseClick:Connect(function(plr)
-- triggers event when a cup is clicked (using a ClickDetector)
if currentTurn == 1 then
-- checking if it is the right turn
if plr == plr1 then
-- checking if the player who clicked is the player that is supposed to swap the cups
if firstcup1 ~= h then
-- checking if the firstcup1 (first cup that they clicked) isnt the same as the second
if firstcup1 ~= nil then
-- checking if they even clicked a first cup, if not, set the first cup
print("swapping "..tostring(firstcup1.TP.BrickColor).." and "..tostring(h.TP.BrickColor))
local firstColor = firstcup1.TP.BrickColor
-- swapping the two cups in the script
tableclone1.CupSpawns[h.Name].AssignedCup.Value = firstColor
tableclone1.CupSpawns[firstcup1.Name].AssignedCup.Value = h.TP.BrickColor
-- swapping the two cups visualy
tableclone1.Cups[h.Name]:SetPrimaryPartCFrame(tableclone1.CupSpawns[firstcup1.Name].CFrame)
tableclone1.Cups[firstcup1.Name]:SetPrimaryPartCFrame(tableclone1.CupSpawns[h.Name].CFrame)
firstcup1 = nil
else
firstcup1 = h
end
end
end
end
end)
end
Help would really be appreciated since this is pretty much the last thing to add before the game is done…
Firstly, format this. Please please do this for solely your benefit. If we can’t read it we can’t fix it, and the same goes for you. I cannot stress how important code organization and the elimination of redundant code is. Sorry if this came out offensive or aggressive. Just looking out for you!
I organized it so it is a lot easier to read, troubleshoot, and debug.
local firstcup1 = nil
for j, Cup in pairs(tableclone1.Cups:GetChildren()) do -- looking through all the cups
Cup.Click.MouseClick:Connect(function(plr) -- triggers event when a cup is clicked (using a ClickDetector)
if currentTurn == 1 and plr == plr1 and firstcup1 ~= Cup and firstcup1 ~= nil then
-- checking if it is the right turn
-- checking if the player who clicked is the player that is supposed to swap the cups
-- checking if the firstcup1 (first cup that they clicked) isnt the same as the second
-- checking if they even clicked a first cup, if not, set the first cup
print("swapping "..tostring(firstcup1.TP.BrickColor).." and "..tostring(Cup.TP.BrickColor))
local firstColor = firstcup1.TP.BrickColor
-- swapping the two cups in the script
tableclone1.CupSpawns[Cup.Name].AssignedCup.Value = firstColor
tableclone1.CupSpawns[firstcup1.Name].AssignedCup.Value = Cup.TP.BrickColor
-- swapping the two cups visually
tableclone1.Cups[Cup.Name]:SetPrimaryPartCFrame(tableclone1.CupSpawns[firstcup1.Name].CFrame)
tableclone1.Cups[firstcup1.Name]:SetPrimaryPartCFrame(tableclone1.CupSpawns[Cup.Name].CFrame)
firstcup1 = nil
else
firstcup1 = Cup
end
end)
end
Ill try to fix this as much as I can from here.
Also, another tip (which you probably already know but I’ll say anyways): name your variables i.e. h, but in this case it’s alright cuz it’s pretty clear what h is.
yeah, those are some good tips, thank you man! I understand how important it is and im trying to get better at it for sure. I just dont know all the methods to eliminate redundant code and all that. I managed to find the error btw, it wasn’t about indentation but still, thank you for answering (:
yea i wasnt implying that the error was in formatting i was just tryna say that devforum can be the best way to get help with roblox lua, but when asking a question, make it easy for the members. i was gonna ask for the tree explorer thing of tableclone1 - nice work tho!