Cant Save Data to a Data Store Service

Writing Data to a Data Store

When trying to either save the data it fails or trying to load the data it fails to load in properly as it is always defaulting back to 0

I have tried changing the way it saves data so far and nothing has worked

Everytime I basically rejoin it fails to save the data and immediatley defaults back to 0 even though I have changed the values on the server side

-- Defining Data Store Service and making a data store
local DataStoreService = game:GetService("DataStoreService")
local playerCashStore = DataStoreService:GetDataStore("playerCashStore")

-- Defining the main group ID used for the rank variable.
local groupID = 10797715

-- Checking if a new player has joined the game
game.Players.PlayerAdded:Connect(function(player)
	
	-- Making a leaderstats folder to save variables such as cash and rank
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	-- Adding cash to the leaderboard
	local cash = Instance.new("IntValue")
	cash.Parent = leaderstats
	cash.Name = "Cash"
	
	-- Adding a rank to the leaderboard
	local rank = Instance.new("StringValue")
	rank.Parent = leaderstats
	rank.Name = "Rank"
	
	-- Making the rank string update when the player joins the game
	rank.Value = player:GetRoleInGroup(groupID)
	
	-- Making a Custom Player ID for when the player joins
	local playerDataStoreKey= "plr_".. player.UserId
	print("Player: ".. player.Name .." Connected Data Store Key: ".. playerDataStoreKey .. " pulled/generated")
	
	-- Loading data from this Data Store from the player key
	local data
	local success, errorMessage = pcall(function()
		data = playerCashStore:GetAsync(playerDataStoreKey)
	end)
	
	-- Checking if loading player data from data store key was a success otherwise throwing warning on failing to load data
	if success then
		print("Data Successfully loaded from data store key: ".. playerDataStoreKey)
		print("Data Loaded: ".. data)
		cash.Value = data
	elseif errorMessage then
		warn("Data Failed to load on data store key: ".. playerDataStoreKey .." Error Code: ".. errorMessage)
	end
end)

-- Checking if a player has left to save data
game.Players.PlayerRemoving:Connect(function(player)
	-- Loading the data store key
	local playerDataStoreKey = "plr_".. player.UserId
	print("Player ".. player.Name .." Removing Writing to Data Store Key: ".. playerDataStoreKey)
	
	local data = player.leaderstats.Cash.Value
	
	-- Saving Data To said data store key
	local success, errorMessage = pcall(function()
		playerCashStore:SetAsync(playerDataStoreKey, data)
	end)
	
	-- Checking if data has failed to be written or not
	if errorMessage then
		warn("Failed to write data to data store key: ".. playerDataStoreKey .." Error Code: ".. errorMessage)
	elseif success then
		print("Data Successfully written to data store key: ".. playerDataStoreKey)
	end
end)

I don’t see any issue with the script. Are all the outputs fine? Perhaps you didn’t enable API services

API Services are on otherwise it would throw an error.

Outputs should be fine. Its saving the value of the Intvalue not the Intvalue instance itself.

Can you take screenshots of the output upon leaving and joining so we can trace the script

When I tried printing the cash value when the player was leaving it did not print the value might be something to do with that

Output:

Right the data isn’t saving because your test server in Roblox studio shuts down before the data can save. Try publishing it and trying it ingame

It works in game yeah. Weird that roblox studio test servers do stuff like that sometimes. Anyways thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.