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

Why does this value not save [DataStore]?

local DataStore = game:GetService("DataStoreService")
local grandopeningkitsds = DataStore:GetDataStore("GrandOpeningKits")

game.Players.PlayerAdded:connect(function(player)
	local stats = Instance.new("Folder",player)
	stats.Name = "stats" -- creates a leaderboard
	
	local statsloaded = Instance.new("BoolValue")
	statsloaded.Parent = player.stats
	statsloaded.Name = "StatsLoaded"

	-- CREATES GRAND-OPENING-KITS VALUE --
	local grandopeningkits = Instance.new("NumberValue")
	
	grandopeningkits.Parent = player.stats
	grandopeningkits.Name = "GrandOpeningKits" -- creates a leaderstat value
	grandopeningkits.Value = grandopeningkitsds:GetAsync(player.UserId) or 0 -- gets past value
	grandopeningkitsds:SetAsync(player.UserId,grandopeningkits.Value) -- sets a new value
	local grandopeningkitnum = grandopeningkits.Value
	
	-- SETS STATSLOADED VALUE TO TRUE --
	statsloaded.Value = true
	
	-- LEAVE BACKUP SAVE --
	game.Players.PlayerRemoving:connect(function(player)
		print("Saving Data for user "..player.UserId)
		grandopeningkitsds:SetAsync(player.UserId,player.stats.GrandOpeningKits.Value)
		print(player.UserId.."'s Data has been saved successfully.")
		if error then
			print("DATA ERROR: "..player.UserId.."'s Data has been saved unsuccessfully")
		end
	end)
end)

I use a similar script on another game and it works fine. Anybody know whats going on?

2 comments

0

If you're just going off of "if error then" It should save. You never defined error so "error" is always the error function. Otherwise can you post an error? Or is there no errors.

0

Perhaps you need to save the value rather than the Value Object. (i.e, line 27 add a .Value to the end of GrandOpeningKit)

1 answer

Hello. I'm not sure if this will solve your problem, but here's the code I have created. (cloned and edited)

local DataStore = game:GetService("DataStoreService")
local grandopeningkitsds = DataStore:GetDataStore("GrandOpeningKits")

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("Folder",player)
    stats.Name = "stats" -- creates a leaderboard

    local statsloaded = Instance.new("BoolValue")
    statsloaded.Parent = player.stats
    statsloaded.Name = "StatsLoaded"
	statsloaded.Value = false
    -- CREATES GRAND-OPENING-KITS VALUE --
    local grandopeningkits = Instance.new("NumberValue")
	repeat wait() print('Waiting for stats folder..') until player.stats
    grandopeningkits.Parent = player.stats
    grandopeningkits.Name = "GrandOpeningKits" -- creates a leaderstat value
	local amt = grandopeningkitsds:GetAsync(player.UserId) 
	if amt == nil then amt = 0 end
    grandopeningkits.Value = amt
    
    -- SETS STATSLOADED VALUE TO TRUE --
    statsloaded.Value = true

    -- LEAVE BACKUP SAVE -- Bad idea. It will cause Datastore limit errors to break the script.
end)

    game.Players.PlayerRemoving:connect(function(player)
        print("Saving Data for user "..player.UserId)
	if player.stats.StatsLoaded.Value == true then
	local success, err = ypcall(function()
        grandopeningkitsds:SetAsync(player.UserId,player.stats.GrandOpeningKits.Value)
	end)
	if success == true then
        print(player.UserId.."'s Data has been saved successfully.")
	else print(player.UserId.."'s Data has been saved unsuccessfully.")
	end
	else
	print(player.UserId.."'s Data has been saved unsuccessfully. Had not loaded.")
	end
    end)

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