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

Why is my table not inserting any values?

local mapVotes = {}
	local playerVotes = {}
	for i, map in pairs(game.ReplicatedStorage.VotedMap:GetChildren()) do
		table.insert(mapVotes, map.Value)
	end
	for i, choice in pairs(game.ReplicatedStorage.VotedPlayers:GetChildren()) do
		table.insert(playerVotes, choice.Value)
	end
	table.sort(mapVotes)
	table.sort(playerVotes)
	print(mapVotes)
	print(playerVotes)

So I've tried to troubleshoot this and I think there's an issue with the for loops. Any ideas?

2 comments

0

It is, you're just not presenting the arrays properly. Use: "print(unpack(array))"

0

Still not working. It only prints 0, even though the values in repstorage changed.

2 answers

yes use unpack to print it, unpack will read each string.

EDIT:

instead of print(unpack(mapVotes)) try looping through every string and printing it, if it doesnt work then i dont know what will:

for _,i in pairs(mapVotes) do

	print(i)

end

EDIT: ok im sorry i didnt know they werer int values, int values cant print, everything makes so much sense. Ok its simple. tostring. new code:

for _,i in pairs(mapVotes) do

	e = tostring(i)
	print(e)

end

i dont know if it works with unpack, try it:

print( tostring( unpack( mapVotes ) ) )

9 comments

0

Still not working. It only prints 0, even though the values in repstorage changed.

0

how do u know they changed

0

The value changes whenever I press the button

0

hmm, it owuld only print 0 if there was nothing in the array

0

the instances in voted players and in votedmap are string values right?

0

no they are int values for a map vote. I'm trying to compare the values and return the highest one

0

OH OK I KNOW THE ANSWER

0

dang. i think the values are being initalized before the vote somehow. thanks for trying to help

0

just try your best you can do it im happy to somewhat help :)

Try a different approach, maybe you made a typo, or an Instance is not defined correctly within the script. Either way, I did re-create the script with new practices that I suggest you use.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local MapVotes = {}
local PlayerVotes = {}

local VotedMap = ReplicatedStorage:WaitForChild("VotedMap")
local VotedPlayer = ReplicatedStorage:WaitForChild("VotedPlayers")

for index, value in pairs(VotedMap:GetChildren()) do
	if value ~= nil and typeof(value) == "Instance" and value:IsA("ValueBase") then
		table.insert(MapVotes, value.Value)
		print("Value has been inserted.")
	end
end

for index, value in pairs(VotedPlayer:GetChildren()) do
	if value ~= nil and typeof(value) == "Instance" and value:IsA("ValueBase") then
		table.insert(PlayerVotes, value.Value)
		print("Value has been inserted.")
	end
end

table.sort(MapVotes)
table.sort(PlayerVotes)
pcall(print, table.concat(MapVotes, ", "))
pcall(print, table.concat(PlayerVotes, ", "))

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