(Solved) Data Store not saving data

Hello, I want to create a DataStore using Data Store V2, the issue is that the Data Store saves, but it always gets the default Value.

This is my code:

local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')

local options = Instance.new('DataStoreOptions') 
options:SetExperimentalFeatures({["v2"] = true})
--options.AllScopes = true

local PointsData = DataStoreService:GetDataStore("PlayerPoints", "global", options)
local LevelData = DataStoreService:GetDataStore("PlayerLevel", "global", options)

local SetOptions = Instance.new('DataStoreSetOptions')
SetOptions:SetMetadata({['PlayerPointsType'] = 'Int'})
SetOptions:SetMetadata({['PlayerLevelType'] = 'Int'})


local function SaveStatsData(Player)
	local Points = Player:WaitForChild("leaderstats").Points.Value
	local Level = Player:WaitForChild("leaderstats").Level.Value

	local PlayerPointsKey = Player.UserId

	local Sucess, Errormessage = pcall(function()
		PointsData:SetAsync(PlayerPointsKey, Points, {Player.UserId}, SetOptions)
		PointsData:SetAsync(PlayerPointsKey, Level, {Player.UserId}, SetOptions)
	end)

	if not Sucess then
		print(Errormessage)
	else
		print('Data Saved')
	end

end

local function LoadPlayerPointsData(Player)

	local PlayerPointsKey = Player.UserId

	local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
		return PointsData:GetAsync(PlayerPointsKey, Player:WaitForChild("leaderstats").Points.Value)
	end)

	if Sucess then
		print("Points:"..CurrentNumber)
		print("Points:"..KeyInfo.Version)
		print("Points"..KeyInfo.CreatedTime)
		print("Points:"..KeyInfo.UpdatedTime)
		print(KeyInfo:GetUserIds())
		print(KeyInfo:GetMetadata())
	else
		print(CurrentNumber)
	end

end

local function LoadPlayerLevelData(Player)

	local PlayerPointsKey = Player.UserId

	local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
		return PointsData:GetAsync(PlayerPointsKey, Player:WaitForChild("leaderstats").Level.Value)
	end)

	if Sucess then
		print("Level:"..CurrentNumber)
		print("Level:"..KeyInfo.Version)
		print("Level"..KeyInfo.CreatedTime)
		print("Level"..KeyInfo.UpdatedTime)
		print(KeyInfo:GetUserIds())
		print(KeyInfo:GetMetadata())
	else
		print(CurrentNumber)
	end

end

Players.PlayerRemoving:Connect(SaveStatsData)
Players.PlayerAdded:Connect(LoadPlayerPointsData)
Players.PlayerAdded:Connect(LoadPlayerLevelData)

Output:
Points: 0 - Server - script:39
Points: (Version) - Server - script:40
Points: (Created Time) - Server - script:41
Points: (Updated Time) - Server - script:42
etc.
Data Saved - Server ← It says that the data store saved.

1 Like

ru making the currency changes client sided?

No, I’m using a normal script in ServerScriptService.

no what i meant is, when you’re testing and changing the currency, are you doing it via client or via server?

if like in studio you’re changing your currency locallly thats why its not saving.

I don’t think thats the problem, I think is something of the script because I change them in the server.

since the datastore is working fine, when you test your game in studio, how are you changing the currency?

I change it via the Explorer, using Players.

you’re probably changing it via client
there is this button in studio in the top bar click it so you can switch to the server.
image
and change your currency
and then leave & rejoin and see if it saves.

I tried that, and it still saves the same value.

Do you have Studio Access to API Services enabled in game permissions?

Yes, it has access to Data Stores.

Actually play the game not in studio

I have this issue in studio but not when I actually play

I also did that and nothing happened.

This isn’t still solved, can someone help?