Repeating functions until Table value is adequate

Howdy howdy, I took a break from coding for a while, being made manager at my job IRL took alot of time so I had no choice. Bored of gaming I’m back. Here’s what I’m doing making this post, basically I’m making a puzzle


I have an easy code I can use to scramble the puzzle but it starts off the same every time, which is really boring for me. Considering I’m probably the only person that’ll ever play the game I want to change this. I’m trying to make a true random. I can do the general concept of the commands. But what I’m having trouble with is insuring a spot hasn’t been used, I’m using a simple table to keep track of each position.

I’ve been finding it hard to find good resources regarding Table functions like this. I appreciate the help. Hopefully this is enough info to keep me from getting a warning or something haha. I don’t usually make posts

Can’t see any issues immediately with the code, however it doesn’t seem like the most effective way to randomise the table. Here is a small bit of code that (hopefully) works better (not tested yet, may have a syntax error or 2.)

local ValTable = {c11, c12, c13, c14, c21, c22, c24, c31, c32, c33, c34, c41, cf2, cf3, emp}

function RandomiseTable(TempTable)
    local ResultTable = {}

    while #TempTable > 0 do
        local RandomValue = math.random(#TempTable)
        table.insert(ResultTable, TempTable[RandomValue])
        table.remove(TempTable,  RandomValue)
    end
    return ResultTable
end

ValTable = RandomiseTable(ValTable)
-- Or you could set a new variable to it:
local RandomTable = RandomiseTable(ValTable)

Hope this proves useful until someone can identify the issue in the initial code

edit: Seems the issue is the until bit. table.find() takes 2 mandatory parameters: the table, and the value to find. You only have one value in the table.find bit, so it doens’t work

1 Like

I’ve had a few edits where I got an error in the output stating it was missing, But that’s the issue, I need it to select the latest value. I’m realizing now that #usedpos probably only spits the number of values in the table, not the latest table. My coding is beginner level I know. But I really want to avoid copying other peoples code. Will it be unavoidable to use while do statements?
Is there a proper way to write the

until table.find

in order to verify its not already used. Or if I simply remove that value will it be repeatable multiple times a game, or would it only run once?

– I appreciate the help

I’ve found a solution though I’m sure there are better ways of doing this. Basically the value is removed from the table after its used, then when you want to scramble again it adds all of them back

ValTable = {c11,c12,c13,c14,c21,c22,c24,c31,c32,c33,c34,c41,c42,c43,emp}
function trurandom()
	print (#ValTable)
	local mathnum = math.random(1, #ValTable)
	trueval = ValTable[mathnum] 
	table.remove(ValTable,mathnum)
	print ("Done", trueval)
end

Below when adding the values back into the table

if not shiftpress() then
	else
		if puz0.Active.Value == true then
			local num = 0
			local tabl = {c11,c12,c13,c14,c21,c22,c24,c31,c32,c33,c34,c41,c42,c43,emp}
			while #ValTable < 15 do
				table.insert(ValTable,tabl[num+1])
				num = num+1
				end
			random()
			SHIFTGui.BackgroundColor = BrickColor.new(0.176471, 0.176471, 0.176471)
		else
		player.Character.Humanoid.WalkSpeed = 30
			SHIFTGui.BackgroundColor = BrickColor.new(0.176471, 0.176471, 0.176471)
		end
	end

Maybe this will help someone besides myself.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.