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

Values getting removed then coming back?

When I try removing the ChosenSong and FakeChosenSong values they print as nil but are used multiple times and they print at the bottom normally. So they don't actually get removed and im not sure what is causing this.

Output GIF: https://gyazo.com/970ecb14b8aa72e84e72ec97010b14ff

songs = {461736208,438917260,516046263,506212392}

boards = workspace.Boards

while true do
	
	chosensong = songs[math.random(1,#songs)] --Choses a random ID from songs list
	chosenboard = math.random(1,4) --Selects random board
	for i = 1,4 do
		if i == chosenboard then
			local songname = game:GetService("MarketplaceService"):GetProductInfo(chosensong) --Gets the asset name from ID
			boards:FindFirstChild("Board"..i):WaitForChild("SurfaceGui"):WaitForChild("SongChoice").Text = tostring(songname.Name) --Places chosen song on boards
			table.remove(songs, chosensong)
			print(songs[chosensong])
		else
			local chosenfakesong = math.random(1,#songs)
			local chosenfakesong = songs[chosenfakesong]
			local songname = game:GetService("MarketplaceService"):GetProductInfo(chosenfakesong) --Gets the asset name from ID
			boards:FindFirstChild("Board"..i):WaitForChild("SurfaceGui"):WaitForChild("SongChoice").Text = tostring(songname.Name) --Places chosen song on boards
			table.remove(songs, chosenfakesong)
			print(songs[chosenfakesong])
		end
	end
	wait(1)
	for i = 1,#songs do
		print(songs[i])
	end
	wait(1)
end

2 answers

✓ Accepted answer

Ok, so, lets start with Line 7

chosensong = songs[math.random(1,#songs)]

This is giving you the number ID for the game to identify/play, not the index number of it. I advise you do something along the lines of

chosensongno = math.random(1,#songs)
chosensong = songs[chosensongno]

Next part is Line 13

table.remove(songs, chosensong)

This is trying to remove a value from the table which does not exist, this is where you would use the value 'chosensongno' making it

table.remove(songs, chosensongno)

You would need to repeat this same process for the fake songs aswell, hope this helps.

1 comment

0

Thanks!

table.remove(table,position) removes whatever item is at the position. Therefore, table.remove(table,object) will not work.

Example:

local songs = {1000,2000,3000}
table.remove(songs,3) --Removes 3000 from the table.
table.remove(songs,2000) --This will NOT remove 2000 from the table, since it tries to remove the 2000th item in the table, not the number 2000.
print(songs[1]) --Prints 1000.
print(songs[2]) --Prints 2000.

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