Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i remove a object from array?

i want to remove the object that math.random takes, to only pick one for each door

example: for door1.mapArea.Value = obby1 for door2.mapArea.Value = obby3 for door3.mapArea.Value = obby2

mapArea is a objectValue

Code:

	while true do 
		wait(1)
	
	local doorsarray = {workspace["Teleporters"].Door1.mapArea, workspace["Teleporters"].Door2.mapArea, workspace["Teleporters"].Door3.mapArea}
	local doorrandom = doorsarray[math.random(1,#doorsarray)]
	
	local obbyarray = {workspace.obby1, workspace.obby2, workspace.obby3}
	local obbyrandom = obbyarray[math.random(1,#obbyarray)]
	
	print(obbyrandom)
	doorrandom.Value = obbyrandom
	
	local function removerandom(array, values)
		for i = #array, 1, -1 do
			if array[i] == values then
				table.remove(array, i)
			end
		end
	end
	removerandom(obbyarray, obbyrandom)
print(#obbyarray)
	end
	

1 answer

✓ Accepted answer

The easiest way to do this is to shuffle the obbyArray and then just go through and assign each value sequentially. Something like this:

local doorsArray = {workspace["Teleporters"].Door1.mapArea, workspace["Teleporters"].Door2.mapArea, workspace["Teleporters"].Door3.mapArea}

local obbyArray = {workspace.obby1, workspace.obby2, workspace.obby3}

-- Function to shuffle a table, found here: https://gist.github.com/Uradamus/10323382
local function Shuffle(tbl)
	for i = #tbl, 2, -1 do
		local j = math.random(i)
		tbl[i], tbl[j] = tbl[j], tbl[i]
	end
	return tbl
end

Shuffle(obbyArray)

for i = 1, #doorsArray do
	doorsArray[i].Value = obbyArray[i]
end

This discussion is preserved as it was. New answers, comments, and votes are closed.