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

[SOLVED] The datastore saves but it still returns nil when i try to load it?

local Players = game.Players
local Datastore = game:GetService("DataStoreService")
local DS = Datastore:GetDataStore("DS")

function LoadFunc(Key)
	
	local s, e = pcall(function()
		
		local Data = DS:GetAsync(Key)
		
		print("Got Data")
		
		return Data
		
	end)
	
	if s then
		
		print("Succesfully Got Data")
		
	else
		
		warn(e)
		
	end
	
end

function SaveFunc(WhichData, Key)
	
	local s, e = pcall(function()
		
		DS:SetAsync(Key, WhichData)
		
	end)
	
	if s then
		
		print("Saved")
		
	else
		
		warn(e)
		
	end
	
end

Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Level = Instance.new("NumberValue")
	Level.Value = 1
	Level.Name = "Level"
	Level.Parent = leaderstats
	
	local LoadedLevel = LoadFunc("Key-Level"..plr.UserId)
	
	if LoadedLevel then
		print("Not nil")
		Level.Value = LoadedLevel
		print("Set Level")
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	SaveFunc(plr:WaitForChild("leaderstats"):WaitForChild("Level").Value, "Key-Level"..plr.UserId)
	
end)

So I tried this and it still saves but it doesn't load, it doesn't print "Not nil" or "Set Level". I tried accessing the datastore using Data Editor V2 and it worked, it showed the value which was 2 and it didn't have any extra value, just 2 like intended. But when I load into the game, the Value of level isn't two, rather, it is one meaning it didn't set the value at all. I am suspecting that its because GetData() is returning nil but I do not know why

3 comments

0

Update: I put else warn("is nil") and it said that, so GetAsync IS returning nil

0

Another update: i put print(Data) in the pcall and it printed 1, which is weird

0

Another update: i put print(Data) in the pcall and it printed 1, which is weird

1 answer

Fixed it! It turns out it was not returning anything, instead I had to make the Data variable outside of the call and then set the data to the Data variable and then check if its nil, if not it returns the data! It works now.

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