Cant compare variable to Value

  1. What do you want to achieve? Keep it simple and clear!
    Making a script that checks if an input of buttons is the same as the intended order(like blue then red then green then yellow, its random each time).
  2. What is the issue? Include screenshots / videos if possible!
    everything works but when I compare my variable “currentorder” to “goodorder”, I get no error and the code never works(even though it should), and I have no idea why(tried debugging, the values are the same)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Switching some other variables, changing “goodorder” to an IntValue and NumberValue, same result.
local yellow = game.Workspace.xylophonestage1:WaitForChild("yellow").ClickDetector
local green = game.Workspace.xylophonestage1:WaitForChild("green").ClickDetector
local red = game.Workspace.xylophonestage1:WaitForChild("red").ClickDetector
local goodorder = game.Workspace.order1
local currentorder = 0
local size = 0


blue.MouseClick:Connect(function()
	size += 1
	
	if currentorder == 0 then
		currentorder = 11
	else
		currentorder = currentorder..size..1
	end
	if size == 4 then
		if currentorder == goodorder.Value then
			print("good job!")
		else
			print("bad job :(")
		end
		print(currentorder.."    "..goodorder.Value)
		size = 0
		currentorder = 0
	end
end)
yellow.MouseClick:Connect(function()
	size += 1
	
	if currentorder == 0 then
		currentorder = 12
	else
		currentorder = currentorder..size..2
	end
	if size == 4 then
		if currentorder == goodorder.Value then
			print("good job!")
		else
			print("bad job :(")
		end
		print(currentorder.."    "..goodorder.Value)
		size = 0
		currentorder = 0
	end
end)
green.MouseClick:Connect(function()
	size += 1
	
	if currentorder == 0 then
		currentorder = 13
	else
		currentorder = currentorder..size..3
	end
	if size == 4 then
		if currentorder == goodorder.Value then
			print("good job!")
		else
			print("bad job :(")
		end
		print(currentorder.."    "..goodorder.Value)
		size = 0
		currentorder = 0
	end
end)
red.MouseClick:Connect(function()
	size += 1
	
	if currentorder == 0 then
		currentorder = 14
	else
		currentorder = currentorder..size..4
	end
	if size == 4 then
		if currentorder == goodorder.Value then
			print("good job!")
		else
			print("bad job :(")
		end

		print(currentorder.."    "..goodorder.Value)
		size = 0
		currentorder = 0
	end
end)

(it always returns bad job)

to get this to work, you should save your ordering as a string that represents the order (not a number! - or you will have to cast it to a number explicitly.)

if your goodorder string doesn’t look something like this 2134, then it definitely should. It just represents the actual ordering.

I will also suggest making a function so you don’t have repeated code all over the place, that is an easy way to make mistakes.

Also, you are concatenating size into the ordering where it is not needed.

Here, try this. It is your script with my suggestions implemented:

local blue = game.Workspace.xylophonestage1:WaitForChild("blue").ClickDetector
local yellow = game.Workspace.xylophonestage1:WaitForChild("yellow").ClickDetector
local green = game.Workspace.xylophonestage1:WaitForChild("green").ClickDetector
local red = game.Workspace.xylophonestage1:WaitForChild("red").ClickDetector

local goodorder = game.Workspace.order1

local currentorder = ""


local function OrderingBehavior(orderingIndex)
	
	currentorder = currentorder .. orderingIndex
	

	if #currentorder < 4 then return end


    if currentorder == goodorder.Value then
		print("good job!")
	else
		print("bad job :(")
	end

	print(currentorder.."    "..goodorder.Value)

	currentorder = ""
end






blue.MouseClick:Connect(function()
	
	OrderingBehavior(1)
end)

yellow.MouseClick:Connect(function()
	
	OrderingBehavior(2)
end)

green.MouseClick:Connect(function()
	
	OrderingBehavior(3)
end)

red.MouseClick:Connect(function()
	
	OrderingBehavior(4)
end)
2 Likes
After each input
task.wait(5)

local ws = game.Workspace:WaitForChild("xylophonestage1")
local b, y = ws.blue.ClickDetector, ws.yellow.ClickDetector
local g, r = ws.green.ClickDetector, ws.red.ClickDetector
local goodorder, current, size = game.Workspace.order1, "", 0

local function order(i)
	current = current .. i ; size += 1
	if goodorder.Value:sub(1, size) ~= current then
		print("wrong order")
		current, size = "", 0
		return
	end
	if size == 4 then
		print("good job!")
		current, size = "", 0
	end
end

b.MouseClick:Connect(function() order("1") end)
y.MouseClick:Connect(function() order("2") end)
g.MouseClick:Connect(function() order("3") end)
r.MouseClick:Connect(function() order("4") end)
After 4 inputs
task.wait(5)

local ws = game.Workspace:WaitForChild("xylophonestage1")
local b, y = ws.blue.ClickDetector, ws.yellow.ClickDetector
local g, r = ws.green.ClickDetector, ws.red.ClickDetector
local goodorder, current, size = game.Workspace.order1, "", 0

local function order(i)
	current = current .. i ; size += 1
	if size == 4 then
		print(current == goodorder.Value and "pass" or "wrong")
		current, size = "", 0
	end
end

b.MouseClick:Connect(function() order("1") end)
y.MouseClick:Connect(function() order("2") end)
g.MouseClick:Connect(function() order("3") end)
r.MouseClick:Connect(function() order("4") end)

Test scripts.

This does fix OPs issue but it also modifies his logic such that it gives an incorrect combo error early if it is incorrect, I just figured I should mention that for OP.

That’s a feature you really don’t want on a combination lock, I should mention. It makes it possible to bruteforce easily by just sequentially testing each button, and seeing if it cuts early or not

It could be exactly what you need for a one of those games where you memorize a pattern, and then play it back. You would want that feature in that case. It just depends on what OP is really doing with the ordering

The main problem was in the StringValue, although your simplification of the code made it perfect, thank you.

1 Like