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)

5 comments
sorry I might have not said as clear as I should have but what I meant is I know that the dataStore is working but for some reason when i start the game my value for my level is 0 and my max xp is at 0 so I am trying to make it when u first join the game you start at Level 1 and have Max Xp at 100 idk why I cant figure this out and it is probably a Easy fix.
O
O
try this: if THING.value == 0 then THING.value = NUMBER end at the end of the playerAdded function
that didnt work but i tried it in a different place and it works now thanks!