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

My Datastore won't save/load data! [?]

so on line 23 I get an error saying: ServerScriptService.Leaderstats:23: attempt to index nil with 'UserId'

local DS = game:GetService("DataStoreService")
local CurrencySave = DS:GetDataStore("FirstStore")


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

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue", leaderstats)
	Money.Name = "Money"
	Money.Value = 100
	
	local XP = Instance.new("IntValue", leaderstats)
	XP.Name = "Level"
	XP.Value = 1
	
	

	local data = 
	local success, errormessage = pcall(function(player)
		data:GetAsync(player.UserId.."-Money")
	end)

	if success then
		Money.Value = data
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
end)

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

	local success, errormessage = pcall (function()
		CurrencySave:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
	end)

	if success then
		print("Player data Saved!")
	else
		print("Player Data didn't Save.")
		warn(errormessage)
	end

end)

2 comments

0

did you opened HTTP serivces to RobloxStudio ?

0

yes

1 answer

So there were a couple of things I noticed.

First, it seems as though you wanted to initialize a local variable named data on line 21 but never actually completed it.

Second, you attempted to use the 'data' variable which was not correctly initialized to get information stored in your FirstStore datastore. Instead, to get the information from the FirstStore datastore, you must use the CurrencySave variable which is equal to the FirstStore datastore.

Third, when you did try to access the datastore using the 'data' variable, you attempted to se the Money variable's value to the 'data' value which, again, was incorrectly initialized.

local DS = game:GetService("DataStoreService")
local CurrencySave = DS:GetDataStore("FirstStore")


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

    local leaderstats = Instance.new("Folder", player) -- The parent is set here
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player -- Why are you setting the parent twice?

    local Money = Instance.new("IntValue", leaderstats)
    Money.Name = "Money"
    Money.Value = 100 -- The default value

    local XP = Instance.new("IntValue", leaderstats)
    XP.Name = "Level"
    XP.Value = 1 -- The default value



    -- local data = 
    -- ^^Not sure why you had this here^^

    local success, errormessage = pcall(function(player)
        local result = CurrencySave:GetAsync(player.UserId.."-Money") -- Get the value from the datastore
	    if result then
		    Money.Value = result -- If there is a value stored on the datastore using the given key, set the value.
	    end
    end)

    if not success then
        print("There was an error while getting your data")
        warn(errormessage)
    end
end)

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

    local success, errormessage = pcall (function()
        CurrencySave:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
    end)

    if success then
        print("Player data Saved!")
    else
        print("Player Data didn't Save.")
        warn(errormessage)
    end

end)

There are some efficiency things that are a bit more complex but I won't go over them here.

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