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

Why wont this auto saving script save the value of DR?

local dss = game:GetService("DataStoreService")
local mds = dss:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
		stats.Name = "leaderstats"
		stats.Parent = player
		
		local DP = Instance.new("IntValue")
		DP.Name = "Points"
		DP.Parent = stats
		
		local DR = Instance.new("IntValue")
		DR.Name = "Dice Rolls"
		DR.Parent = stats

	local data
	local success, errormessage = pcall(function()
		data = mds:GetAsync(player.UserId.."-DR")
	end)
	
	if success then 
		DR.Value = data
	else
		print("Oops! Your data hit an error while loading!")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	 local success, errormessage = pcall(function()
		mds:SetAsync(player.UserId.."-Dice Rolls",player.leaderstats.DR.Value)
	end)
	
	if success then 
		print("Player data Saved!")
	else
		print("Dang it! The data did not save!")
		warn(errormessage)
	end

end)

1 comment

0

Are you testing this in studio or in actual roblox

1 answer

When doing :SetAsync I have realized you inputted a different key than what was at the top of your scripts when trying to Request the data.

Here is the script fixed.

local dss = game:GetService("DataStoreService")
local mds = dss:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("Folder")
        stats.Name = "leaderstats"
        stats.Parent = player

        local DP = Instance.new("IntValue")
        DP.Name = "Points"
        DP.Parent = stats

        local DR = Instance.new("IntValue")
        DR.Name = "Dice Rolls"
        DR.Parent = stats

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

    if success then 
        DR.Value = data
    else
        print("Oops! Your data hit an error while loading!")
        warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
     local success, errormessage = pcall(function()
        mds:SetAsync(player.UserId.."-DR", player.leaderstats.["Dice Rolls"].Value) --2 errors here First you set the wrong key, you also put player.leaderstats.DR which is nil. So I fixed those 2.
    end)

    if success then 
        print("Player data Saved!")
    else
        print("Dang it! The data did not save!")
        warn(errormessage)
    end

end)

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