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

Http:JSONDecode: Argument 1 is missing or nil?

Http:JSONDecode error: Argument 1 is missing or nil

I've been trying to do a script where you can save a table to a datastore. Here is my current script:

game.Players.PlayerAdded:Connect(function(player)
	if ds:GetAsync(player.UserId) ~= nil then
		local toLoad = Http:JSONDecode(ds:GetAsync(player.UserId))
	
		local statStats = toLoad.stats
		local cashCash = toLoad.cash
		
		if statStats == nil then
			local stats = Instance.new("Folder")
			stats.Name = "leaderstats"
			stats.Parent = player
			
			local cash = Instance.new("IntValue")
			cash.Name = "Cash"
			cash.Parent = stats
			
			wait(10)
			
			cash.Value = math.random(1,9999999)
			
			wait(5)
		
			repeat
				cash.Value = math.random(1,9999999)
				be:Fire(cash.Value, player.Name)
				wait(5)
			until player.Parent == nil
		
			game.Players.PlayerRemoving:Connect(function(plr)
				local save = {}
				table.insert(save, 1, cash)
				table.insert(save, 2, stats)
				
				ds:SetAsync(plr.UserId, save)
			end)
		elseif (statStats ~= nil and statStats:IsA("Folder")) or statStats ~= nil then
			statStats.Name = "leaderstats"
			statStats.Parent = player
			
			cashCash.Name = "Cash"
			cashCash.Parent = statStats
			
			wait(10)
			
			repeat
				wait(5)
				cashCash.Value = math.random(1,9999999)
				be:Fire(cashCash.Value, player.Name)
			until player.Parent == nil
			
			game.Players.PlayerRemoving:Connect(function(plr)
				local save = {}
				table.insert(save, 1, cashCash)
				table.insert(save, 2, statStats)
				
				ds:SetAsync(plr.UserId, Http:JSONEncode(save))
				
				warn(ds:GetAsync(plr.UserId))
			end)
		end
	else
		warn("Datastore is ", nil, "!")
		
		local stats = Instance.new("Folder")
		stats.Name = "leaderstats"
		stats.Parent = player
		
		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Parent = stats
		
		wait()
		
		cash.Value = math.random(1,9999999)
		
		wait(5)
		
		repeat
			cash.Value = math.random(1,9999999)
			be:Fire(cash.Value, player.Name)
			wait(5)
		until player.Parent == nil
		
		game.Players.PlayerRemoving:Connect(function(plr)
			local save = {}
			table.insert(save, 1, cash)
			table.insert(save, 2, stats)
			
			ds:SetAsync(plr.UserId, save)
		end)
	end
end)

Everytime i try to get the table, it returns nil. Someone know why?

1 comment

0

The issue is you are trying to Decode the Datastore and not the Encoded Table

1 answer

Well, instead of using JSON in table, you can save it simply.

-- DS is assumable as your Datastore is not mentioned
local DS = game;GetService("DataStoreService"):GetDataStore("BLAHBLAHBLAH")

local save = {}
table.insert(save, 1, cashCash)
table.insert(save, 2, statStats)

DS:SetAsync(plr.UserId, save)

-- And to get your data, you can do:
local toLoad = DS:GetAsync(player.UserId)

Lemme know if it helps!

2 comments

0

but what about the utf-8 error? (datastore only saves utf-8 characters)

0

Well, if you are getting utf-8 error, it might be bcz you are trying to store instances. Just, try saving the value of Cash and Stat. When the player joins, just put that value in the leaderstats folder

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