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

Bad Argument #1 to "pairs" (table expected, got string) How can I fix?

Asked by 5 years ago
Edited 5 years ago

Hello there I got this error: Bad Argument #1 to "pairs" (table expected, got string) so I actually tried to save a IntValue with Name and Value so and if I print the string it says [null] How can I fix that? Line 31 causes the issue after the error^

local DS = game:GetService("DataStoreService")

local PetDS = DS:GetDataStore("PetDataStore")

local PetsTable = {}

local AUTOSAVE_INTERVAL = 60



game.Players.PlayerAdded:Connect(function(Player)

local PetsFolder = Instance.new("Folder")

PetsFolder.Name = "Pets"

PetsFolder.Parent = Player

local key = Player.UserId

print(key)

PetsTable = PetDS:GetAsync(key)

if PetsTable then

print(PetsTable)

game:GetService("HttpService"):JSONDecode(PetsTable)

for _,p in pairs (PetsTable) do

local In = Instance.new("IntValue")

In.Name = p["Name"]

In.Parent = PetsFolder

In.Value = p["Value"]

end

end

end)



game.Players.PlayerRemoving:Connect(function(Player)

PetsTable = {}

local key = Player.UserId

print(key)

for _,i in pairs (Player.Pets:GetChildren()) do

if i then

table.insert(PetsTable,i)

PetsTable[i] = {}

table.insert(PetsTable[i],"Name")

table.insert(PetsTable[i], "Value")

PetsTable[i]["Name"] = i.Name

PetsTable[i]["Value"] = i.Value

print(PetsTable[i]["Name"])

end

end

PetDS:SetAsync(key, game:GetService("HttpService"):JSONEncode(PetsTable))

end)

Thanks for answers!

0
Post which line causes the issue. Also it is redundant to JSON encode a table to save to data stores; this is done by default. User#24403 69 — 5y
0
Line 31 causes the issue let me edit it. Oh and it can't save it cause it is a Array so I need to Encode it per JSON. GodOf_Lua 25 — 5y
0
I posted the solution IceAndNull 142 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Instead of just game:GetService("HttpService"):JSONDecode(PetsTable) do PetsTable = game:GetService("HttpService"):JSONDecode(PetsTable) JSONDecode doesn't change the value, it just returns the new one

0
Sorry, I didn't realize what JSONDecode does at first. This should fix it IceAndNull 142 — 5y
0
Now it dosen't saves just says: [null] when I try to print the json it says null GodOf_Lua 25 — 5y
0
Also if I try it dosen't works and the error is gone GodOf_Lua 25 — 5y
Ad

Answer this question