I need help with my script

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:

robloxapp-20240618-1301349.wmv (356.6 KB)

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…

2 Likes

you have alot of nested ifs. I would look into using the keywords: and, or. Do some research on those.

1 Like

yeah… I do know what “and” and “or” is but i just thought it was better practice to do a bunch of nested ifs… thanks, i’ll look into it!

You can use and if your not doing anything in-between the two if statments

I think your issue is you don’t have enough indentations and nested scopes and whitespace. You should try adding more, see if that works.

1 Like

what do you mean by that? sorry, i dont understand

i mean, how would I add more of them?

hes being sarcastic because you have way too much space and indentation

you should do something like this for multiple conditions

if
  condition1 and
  condition2 or
  condition3
then
  print("test")
end

alright, thank you man. I feel dumb for not getting that sarcasm smh.

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!

are you still working on this game?

yes! i got demotivated after this error, but I am back on track now (:

I am almost done with this. I just need to finish the round system, and then add a shop and such.

1 Like

ok good luck!

(min character count)