Hello users of devforum. I have been following a couple of tutorials of how to use datastoreservice for my game that is gonna use a level system. The issue im having is. It doesn’t save. Whenever I play and get to a specific level. I replay again and now its back to square one.
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local CurrentLevel = player:WaitForChild("Level")
local CurrentEXP = CurrentLevel:WaitForChild("Current")
local MaximumEXP = CurrentLevel:WaitForChild("Max")
local playerUserID = "Player_".. player.UserId
local data = PlayerData:GetAsync(playerUserID)
if data then
CurrentLevel.Value = data
CurrentEXP.Value = data
MaximumEXP.Value = data
else
CurrentLevel.Value = 1
CurrentEXP.Value = 0
MaximumEXP.Value = 100
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player"..player.UserId
PlayerData:SetAsync(playerUserId, player.Level.Value, player.Level.Current.Value, player.Level.Max.Value)
end)
if not success then
warn("Data failed to save")
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(playerInstance)
local statsFolder = Instance.new("Folder", playerInstance)
statsFolder.Name = "Level"
local currentExp = Instance.new("IntValue", statsFolder)
currentExp.Name = "Current"
local maximumExp = Instance.new("IntValue", statsFolder)
maximumExp.Name = "Maximum"
-- Loading Data
local playerId = playerInstance.UserId
local playerData = dataStore:GetAsync(playerId)
if playerData then
-- Loading Saved Data
currentExp.Value = playerData['Current']
maximumExp.Value = playerData['Maximum']
-- Log
print("Loaded Saved Data")
else
-- Loading Default Data
currentExp.Value = 0
maximumExp.Value = 0
-- Log
print("Loaded Default Data")
end
end)
game.Players.PlayerRemoving:Connect(function(playerInstance)
local playerId = playerInstance.UserId
-- Creating Table
local function create_table(playerInstance)
local p_stats = {}
for _, stats in pairs(playerInstance.Level:GetChildren()) do
p_stats[stats.Name] = stats.Value
end
return p_stats
end
-- Saving
local function onplayerExit(playerInstance)
local p_stats = create_table(playerInstance)
local s, e = pcall(function()
dataStore:SetAsync(playerId, p_stats)
print("Saved Data")
end)
if not s then
warn("Couldn't Save Data For: "..playerId)
end
end
onplayerExit(playerInstance)
end)
Put this in a Script in ServerScriptService and it should work.
Also make sure API Services have access to the game.
Edit: I tested it in a game and it worked fine for me.
while the prints do work it doesnt not seem to actually save the data itself. I can see that whenever I get to like for say level 3. I stop the game. Then join back and its back to level 1.
aaaand now for some reason the function for the player to automatically get 25 xp after every 5 seconds or so doesn’t work when I join the game via the website but works whenver I launch in via studio…
You can get it into your game by just getting the model and inserting it, just keep in mind that it could not work if you already have a stats script interfering with it.
Convert it to a Table. I have tested the save for some time now and, it seems to be running smoothly now.
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local CurrentLevel = player:WaitForChild("Level")
local CurrentEXP = CurrentLevel:WaitForChild("Current")
local MaximumEXP = CurrentLevel:WaitForChild("Max")
local playerUserID = "Player_".. player.UserId
local data = PlayerData:GetAsync(playerUserID)
if data then
CurrentLevel.Value = data
CurrentEXP.Value = data
MaximumEXP.Value = data
else
CurrentLevel.Value = 1
CurrentEXP.Value = 0
MaximumEXP.Value = 100
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player_"..player.UserId
local valuetoSave = {player.Level.Value, player.Level.Current.Value, player.Level.Max.Value}
PlayerData:SetAsync(playerUserId, valuetoSave)
end)
if not success then
warn(err)
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)