Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is my table not inserting any values?

Asked by 3 years ago
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?

0
It is, you're just not presenting the arrays properly. Use: "print(unpack(array))" Ziffixture 6913 — 3y
0
Still not working. It only prints 0, even though the values in repstorage changed. BCthegreat407 12 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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 ) ) )
0
Still not working. It only prints 0, even though the values in repstorage changed. BCthegreat407 12 — 3y
0
how do u know they changed AlexanderYar 788 — 3y
0
The value changes whenever I press the button BCthegreat407 12 — 3y
0
hmm, it owuld only print 0 if there was nothing in the array AlexanderYar 788 — 3y
View all comments (5 more)
0
the instances in voted players and in votedmap are string values right? AlexanderYar 788 — 3y
0
no they are int values for a map vote. I'm trying to compare the values and return the highest one BCthegreat407 12 — 3y
0
OH OK I KNOW THE ANSWER AlexanderYar 788 — 3y
0
dang. i think the values are being initalized before the vote somehow. thanks for trying to help BCthegreat407 12 — 3y
0
just try your best you can do it im happy to somewhat help :) AlexanderYar 788 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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, ", "))

Answer this question