I am trying to make a data save script for my game that saved 2 values, the players max health and their health regeneration. The two scripts work fine when I ma just leaving and joining the game, but once I change the stat values in game, the values do not update the next time I load the game. There are no errors in the output.
The script to load the data in when a player joins:
--variables
DSS=game:GetService("DataStoreService")
datastore=DSS:GetDataStore("Character Stats")
function givePlayerKey(player)
local key="player_"..player.UserId
print("Character Stat Datastore Key is: "..key)
return key
end
function loadData(key)
local data=datastore:GetAsync(key)
return data
end
game.Players.PlayerAdded:Connect(function(player)
local loadData=loadData(givePlayerKey(player))
local statfolder=Instance.new("Folder",player)
statfolder.Name="Character Stats"
local maxhealth=Instance.new("IntValue",statfolder)
maxhealth.Name="Max Health"
local currenthealth=Instance.new("IntValue",statfolder)
currenthealth.Name="Current Health"
local healthregen=Instance.new("IntValue",statfolder)
healthregen.Name="Health Regeneration"
if loadData then
print("Character Stat Data was successfully loaded in")
maxhealth.Value=loadData[1]
currenthealth.Value=loadData[1]
healthregen.Value=loadData[2]
else
print("No character stat data found - using default player stats")
maxhealth.Value=10
currenthealth.Value=10
healthregen.Value=0
end
end)
The script that saves data when the player leaves:
--variables
DSS=game:GetService("DataStoreService")
datastore=DSS:GetDataStore("Character Stats")
function givePlayerKey(player)
local key="player_"..player.UserId
print("Character Stat Datastore Key is: "..key)
return key
end
function indexPlayerData(player)
local statfolder=player:WaitForChild("Character Stats")
local maxhealth=statfolder:WaitForChild("Max Health").Value
local healthregen=statfolder:WaitForChild("Health Regeneration").Value
local data={
maxhealth,
healthregen
}
return data
end
function saveData(key,data)
datastore:setAsync(key,data)
end
game.Players.PlayerRemoving:Connect(function(player)
saveData(givePlayerKey(player),indexPlayerData(player))
print("Character Stat Data successfully saved")
end)
Any help is appreciated thank u.

13 comments
How do u change the values? Because datastores save values changed by only the server, not client (local scripts change the value through the client, and normal scripts change on the server). So basically, the server doesn't see changes made by the client.
I do not have anything in place to change the values yet, so I went in the folder to the int value and changed its value manually. Shouldn't the server recognize this change?
Wait a minute... Datastores only work in-game, data doesn't save on studio. Try publishing your game and enabling API services access and then playing through the roblox client.
I did publish the game and enabled API. Does the datastore not work in studio even if these things are on?
It only loads data. So if you play on the roblox client and then save, you go to studio and the saved data is there. But if you leave on studio it won't save, so when u rejoin the saved data will be the data saved on the roblox client.
oh ok that could by why. Let me go try it on roblox. Other than that, does the code look ok?
Umm... I don't use functions on my datastore so I can't really tell much. But going by the logic it looks ok.
Did you already try it on?
It actually worked. I went in game and changed max health to 20 in the console and then when I reloaded the game and printed max health values it said 20. this means datastore is working correctly right?
Yes, it is. Make sure in the future to make sure you don't change the values through the client. I suggest using remote events because you can fire them in a local script and they communicate with the server.
Will do. Thanks for the help
Np. Just make sure to not delete this post because this might help other ppl.
Make sure to put [Solved] in the title to mark this as solved.