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

I'm having a problem using tables in datastores, can you help me fix it?

Asked by
epoke466 100
3 years ago

I'm making a shop that uses a module script, here is the script:

local DataStoreService = game:GetService("DataStoreService")
local PlayerAntemDataStore = DataStoreService:GetDataStore("PlayerAnthem")
local OwnedAnthems = DataStoreService:GetDataStore("OwnedPlayerAnthems")

local module = {}

function module:PurchaseAnthem(Player, PlayerAnthemID, Price, AnthemName)
    print("Somthing was clicked.")
    local CurrentId = PlayerAntemDataStore:GetAsync(Player.UserId)
    if (CurrentId ~= PlayerAnthemID) then
        if (Player.leaderstats.Coins.Value >= Price) then
            Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - Price
            PlayerAntemDataStore:SetAsync(Player.UserId, PlayerAnthemID)
            local Anthems = OwnedAnthems:GetAsync(Player.UserId)
            print("Got Tabel Async")
            local AnthemTabel = {}
            print("MadeTabel") --The error is right arround here.
            for i, v in pairs(AnthemName) do
                table.insert(AnthemTabel, v)
            end
            print("Edited Tabel")
            OwnedAnthems:SetAsync(AnthemTabel)
            print("SavedTabel")
            local m = Instance.new("Message", Player.PlayerGui)
            m.Text = "Purchace Was Succes!"
            wait(3)
            m:Destroy()
        elseif (Player.leaderstats.Coins.Value < Price) then
            local m = Instance.new("Message", Player.PlayerGui)
            m.Text = "You Do Not Have Enough Coins To Purchace This Player Anthem!"
            wait(3)
            m:Destroy()
        else 
            local m = Instance.new("Message", Player.PlayerGui)
            m.Text = "Unknown Error!"
            wait(3)
            m:Destroy()
        end
    else
        local m = Instance.new("Message", Player.PlayerGui)
        m.Text = "This already is your player anthem!!! (noob)"
        wait(3)
        m:Destroy()
    end

end
return module

The script is not working.

I'm getting an error: invalid argument #1 to 'pairs' (table expected, got string)

I think that that means that it is finding a string instead of a table but overall I am really confused. This is the first time I have used tables and I just figured out how to use a data store. Can someone please help me.

1 answer

Log in to vote
0
Answered by 3 years ago

The error is on line 18, you are using a for loop to loop through a string. You can't use a for loop to loop through a string, only through a table.

for i, v in pairs(AnthemName) do -- anthemName is a string.
        table.insert(AnthemTabel, v)
end

What are you trying to do with that for loop?

Ad

Answer this question