Hello. I am currently developing a game in which I have tried implementing an XP system however the levels nor the experience seems to save even though I have implemented a data storage system. There are no errors in the output whatsoever. Please help me.
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Again”)
local lvlupevent = game.ReplicatedStorage.LvlUp
game.Players.PlayerAdded:Connect(function(player)
key1 = Instance.new("IntValue", player)
key1.Name = "key"
played = Instance.new("BoolValue",player)
played.Name = "played"
tutorialDone = Instance.new("IntValue", player)
tutorialDone.Name = "tutorialDone"
statis = Instance.new("IntValue", player)
statis.Name = "leaderstats"
level = Instance.new("IntValue", statis)
level.Name = "level"
money = Instance.new("IntValue", statis)
money.Name = "money"
kills = Instance.new("IntValue", statis)
kills.Name = "kills"
deaths = Instance.new("IntValue", statis)
deaths.Name = "deaths"
griffith = Instance.new("IntValue", player)
griffith.Name = "griffith"
exp = Instance.new("IntValue", player)
exp.Name = "exp"
reqexp = Instance.new("IntValue", player)
reqexp.Name = "reqexp"
reqexp.Value = level.Value * 100
exp.Changed:Connect(function(changed)
if exp.Value >= reqexp.Value then
lvlupevent:FireClient(player)
exp.Value = 0
reqexp.Value = level.Value * 100
level.Value = level.Value + 1
end
end)
local key = "player-"..player.UserId
local savedValues = DataStore:GetAsync(key)
if savedValues then
money.Value = savedValues[1]
kills.Value = savedValues[2]
deaths.Value = savedValues[3]
key1.Value = savedValues[4]
tutorialDone.Value = savedValues[5]
griffith.Value = savedValues[6]
level.Value = savedValues[7]
exp.Value = savedValues[8]
else
local valuesToSave = {money.Value, kills.Value, deaths.Value, key.Value, tutorialDone.Value, griffith.Value, level.Value, exp.Value}
DataStore:SetAsync(key, valuesToSave)
end
end)
The other script:
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Again”)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player-".. player.UserId
local ValuesToSave = {player.leaderstats.money.Value, player.leaderstats.kills.Value, player.leaderstats.deaths.Value, player.key.Value, player.tutorialDone.Value, player.griffith.Value, player.exp.Value, player.leaderstats.level.Value}
pcall(DataStore:SetAsync(key, ValuesToSave))
end)