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

DataStoreService Error: String Expected, Got Table?

Asked by 4 years ago

Hey guys,

Let me place my big chunky script first.


local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("TrialShopDataStore") game.Players.PlayerAdded:Connect(function(player) wait() local data local OwnedTrailsFolder = game.ReplicatedStorage[player.Name..'OwnedTrails'] local Trail1 = OwnedTrailsFolder['#1 Black Trail'] local Trail2 = OwnedTrailsFolder['#2 Blue Trail'] local Trail3 = OwnedTrailsFolder['#3 Green Trail'] local Trail4 = OwnedTrailsFolder['#4 Purple Trail'] local Trail5 = OwnedTrailsFolder['#5 Red Trail'] local Trail6 = OwnedTrailsFolder['#6 Pink Trail'] local Trail7 = OwnedTrailsFolder['#7 Nyan Cat Trail'] local Trail8 = OwnedTrailsFolder['#8 Flag of the Netherlands'] local EquippedTrail = game.ReplicatedStorage[player.Name..'OwnedTrails'].EquippedTrail local Table1 = DataStore:GetAsync(player.UserId) local success, errormessage = pcall(function() warn('#1') --data = myDataStore:GetAsync(player.UserId.."-cash") local Table1 = DataStore:GetAsync(player.UserId) if Table1 then wait() Trail1.Value = Table1[1] Trail2.Value = Table1[2] Trail3.Value = Table1[3] Trail4.Value = Table1[4] Trail5.Value = Table1[5] Trail6.Value = Table1[6] Trail7.Value = Table1[7] Trail8.Value = Table1[8] local TableOneNine = tostring(Table1[9].Value) print('Table1[9] Before ToString: ',Table1[9].Value,'. After ToString: ',TableOneNine) -- Prints Twice 'nil' EquippedTrail.Value = Table1[9] else warn('#3') end warn('#4') end) if success then else warn("There was an error whilst getting your data") warn(errormessage) -- This error message = 36: Invalid Argument #3 (String expected, got table) end end) game.Players.PlayerRemoving:Connect(function(player) local OwnedTrailsFolder = game.ReplicatedStorage[player.Name..'OwnedTrails'] local Trail1 = OwnedTrailsFolder['#1 Black Trail'] local Trail2 = OwnedTrailsFolder['#2 Blue Trail'] local Trail3 = OwnedTrailsFolder['#3 Green Trail'] local Trail4 = OwnedTrailsFolder['#4 Purple Trail'] local Trail5 = OwnedTrailsFolder['#5 Red Trail'] local Trail6 = OwnedTrailsFolder['#6 Pink Trail'] local Trail7 = OwnedTrailsFolder['#7 Nyan Cat Trail'] local Trail8 = OwnedTrailsFolder['#8 Flag of the Netherlands'] local EqTrail local EquippedTrail = game.ReplicatedStorage[player.Name..'OwnedTrails'].EquippedTrail local Save = {} local success, errormessage = pcall(function() table.insert(Save, #Save+1, {player.UserId .. "Trail1", Trail1.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail2", Trail2.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail3", Trail3.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail4", Trail4.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail5", Trail5.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail6", Trail6.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail7", Trail7.Value}) table.insert(Save, #Save+1, {player.UserId .. "Trail8", Trail8.Value}) EquippedTrail = tostring(EquippedTrail.Value) print('EquippedTrail = ',EquippedTrail) wait() table.insert(Save, #Save+1, {player.UserId .. "EquippedTrail", EquippedTrail}) wait() DataStore:SetAsync(player.UserId, Save) end) if success then else warn('IMPORTANT: ',errormessage) -- No error-message when saving! end end)

THE ERROR MESSAGE: "36: Invalid Argument #3 (String expected, got table)"

I'm new to DataStoreService & new to Table's, Rip me. I've tried to use 'ToString' and stuff, but i seem not be able to fix it. I'm not sure what the errors means, nor how to fix it.

I'm sorry for my grammar. I'm Dutch. So English ain't my native language.

0
try doing `print(table1[9])` and see what it outputs. krowten024nabrU 463 — 4y
0
It's printing the classic long table code: 0xd09053a1f9b17169 HeadlessDeathSpeaker 9 — 4y
0
try doing `print(table1[9][1])` and tell us what goes out. krowten024nabrU 463 — 4y
0
Line 36 is an 'else' statement, how can it error? Also it has nothing to do with data stores that's what i already see. imKirda 4491 — 4y

2 answers

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
4 years ago
Edited 4 years ago

DataStores simply cannot store raw table data, only strings. So you must transform the table into a string. Your best option is to use HTTPService:JSONEncode().

Ex.

local tab = {"a", "b", "c"}

local json = HTTPService:JSONEncode(tab)

DataStore:SetAsync(player.UserId, json)

When you want to retrieve the table you can do so with HTTPService:JSONDecode().

Ex.

local json = DataStore:GetAsync(player.UserId)

local tab = HTTPService:JSONDecode(json)

BTW, your English is perfect. Other then apostrophes being used for plural words when they are supposed to be used with possessive words.

Ad
Log in to vote
-2
Answered by 4 years ago

Should work :

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TrialShopDataStore")


game.Players.PlayerAdded:Connect(function(player)
    wait()
    local data
    local OwnedTrailsFolder = game.ReplicatedStorage[player.Name..'OwnedTrails']
    local Trail1 = OwnedTrailsFolder['#1 Black Trail']
    local Trail2 = OwnedTrailsFolder['#2 Blue Trail']
    local Trail3 = OwnedTrailsFolder['#3 Green Trail']
    local Trail4 = OwnedTrailsFolder['#4 Purple Trail']
    local Trail5 = OwnedTrailsFolder['#5 Red Trail']
    local Trail6 = OwnedTrailsFolder['#6 Pink Trail']
    local Trail7 = OwnedTrailsFolder['#7 Nyan Cat Trail']
    local Trail8 = OwnedTrailsFolder['#8 Flag of the Netherlands']
    local EquippedTrail = game.ReplicatedStorage[player.Name..'OwnedTrails'].EquippedTrail
    local Table1 = DataStore:GetAsync(player.UserId)
    local success, errormessage = pcall(function()
        warn('#1')
        --data = myDataStore:GetAsync(player.UserId.."-cash")
        local Table1 = DataStore:GetAsync(player.UserId)
        if Table1 then
            wait()
            Trail1.Value = Table1[1]
            Trail2.Value = Table1[2]
            Trail3.Value = Table1[3]
            Trail4.Value = Table1[4]
            Trail5.Value = Table1[5]
            Trail6.Value = Table1[6]
            Trail7.Value = Table1[7]
            Trail8.Value = Table1[8]
            local TableOneNine = tostring(Table1[9].Value)
            print('Table1[9] Before ToString: ',Table1[9].Value,'. After ToString: ',TableOneNine) -- Prints Twice 'nil' 
            EquippedTrail.Value = Table1[9] else

            warn('#3')
        end
        warn('#4')
    end)
    if success then
    else
        warn("There was an error whilst getting your data")
        warn(errormessage) -- This error message = 36: Invalid Argument #3 (String expected, got table)
    end 
end)

game.Players.PlayerRemoving:Connect(function(player)
    local OwnedTrailsFolder = game.ReplicatedStorage[player.Name..'OwnedTrails']
    local Trail1 = OwnedTrailsFolder['#1 Black Trail']
    local Trail2 = OwnedTrailsFolder['#2 Blue Trail']
    local Trail3 = OwnedTrailsFolder['#3 Green Trail']
    local Trail4 = OwnedTrailsFolder['#4 Purple Trail']
    local Trail5 = OwnedTrailsFolder['#5 Red Trail']
    local Trail6 = OwnedTrailsFolder['#6 Pink Trail']
    local Trail7 = OwnedTrailsFolder['#7 Nyan Cat Trail']
    local Trail8 = OwnedTrailsFolder['#8 Flag of the Netherlands']
    local EqTrail
    local EquippedTrail = game.ReplicatedStorage[player.Name..'OwnedTrails'].EquippedTrail
    local Save = {}
    local success, errormessage = pcall(function()
        table.insert(Save, #Save+1, {player.UserId .. "Trail1", Trail1.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail2", Trail2.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail3", Trail3.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail4", Trail4.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail5", Trail5.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail6", Trail6.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail7", Trail7.Value})
        table.insert(Save, #Save+1, {player.UserId .. "Trail8", Trail8.Value})
        EquippedTrail = tostring(EquippedTrail.Value)
        print('EquippedTrail = ',EquippedTrail) 
        wait()
        table.insert(Save, #Save+1, {player.UserId .. "EquippedTrail", EquippedTrail})
        wait()
        DataStore:SetAsync(player.UserId, Save)
    end)

    if success then

    else
        warn('IMPORTANT: ',errormessage) -- No error-message when saving! 
    end

end)
0
You just copied the code and changed nothing? User#834 0 — 4y
0
please stop posting Optikk 499 — 4y

Answer this question