I am trying to make it when u join you start at level 1 and max exp is 100 but when I start all values = 0 I have tried setting the stat values to this and also tried putting else level.value = 1 etc under the if success part my question is how would I make it that when you play you start at level 1 and max exp 100 here is the code for my data store
local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue", leaderstats) Level.Name = "Level" local exp = Instance.new("IntValue", leaderstats) exp.Name = "Current" local maxExp = Instance.new("IntValue", leaderstats) maxExp.Name = "Max" local Strength =Instance.new("IntValue",leaderstats) Strength.Name = "Strength" Strength.Value = 0 --level change at max exp-- exp.Changed:Connect(function(val) if exp.Value >= maxExp.Value then Level.Value = Level.Value + 1 exp.Value = exp.Value - maxExp.Value maxExp.Value = maxExp.Value * 1.25 Strength.Value = Strength.Value + 5 end end) local levelData local xpData local maxData local success,errormessage = pcall (function() levelData = playerData:GetAsync(player.UserId..("Level")) xpData = playerData:GetAsync(player.UserId..("Current")) maxData = playerData:GetAsync(player.UserId..("Max")) end) if success then Level.Value = levelData exp.Value = xpData maxExp.Value = maxData else warn("there was an loading error".. errormessage) end game.Players.PlayerRemoving:Connect(function(player) local success,errormessage = pcall (function() playerData:SetAsync(player.UserId.."Level",player.leaderstats.Level.Value) playerData:SetAsync(player.UserId.."Current",player.leaderstats.Current.Value) playerData:SetAsync(player.UserId.."Max",player.leaderstats.Max.Value) end) if success then print("Data Has been saved successfuly") else warn("there was an error saving") end
end)
end)
game.Players.PlayerRemoving:Connect(function(player) local success,errormessage = pcall (function() local lead = player:WaitForChild("leaderstats") playerData:SetAsync(player.UserId.."Level",lead.Level.Value) playerData:SetAsync(player.UserId.."Current",lead.Current.Value) playerData:SetAsync(player.UserId.."Max",lead.Max.Value) end) end)
I don't know why, but something like this ^ normally works for me. You might need to use WaitForChild() for all three lines. I hope this helps but if you want an exact error message, join your game with a friend and see what happens when the friend joins and leaves in the dev console.
Additionally, if you are testing in studio, make sure studio has access to api services (in game settings under security)