So, in a past post I was talking about a specific part of this script that was about “LEVEL UP SYSTEM” I added a free script to this system made by me ie I took the Custom DataStore and added the level up system of my own authorship and the script is located on the ServerScriptService.
The problem is that the script doesn’t save my stats
here goes the script:
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStats")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(Plr)
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Plr
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 1
Level.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = 0
XP.Parent = leaderstats
local Rank = Instance.new("StringValue")
Rank.Name = "Rank"
Rank.Value = "Genin"
Rank.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 10
Coins.Parent = leaderstats
local FC = Instance.new("IntValue")
FC.Name = "FC"
FC.Value = 100
FC.Parent = leaderstats
local key = "user-" .. Plr.userId
local storeditems = datastore:GetAsync(key)
if storeditems then
Level.Value = storeditems[1]
Rank.Value = storeditems[2]
XP.Value = storeditems[3]
Coins.Value = storeditems[4]
FC.Value = storeditems[5]
else
local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
datastore:SetAsync(key, items)
end
while wait() do
wait(.01)
if XP.Value >= (100 * (Level.Value + 1)) then
Level.Value = Level.Value + 1
XP.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(Plr)
end
end
end)
game.ReplicatedStorage.AddXP.OnServerEvent:Connect(function(plr)
plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 50
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)
This could be as simple as your game settings are not allow DataStore to saving in studio. Check out this page for more information. If that doesn’t work I can whip up a functioning script and I’ll help you out.
There may be a possibility that ROBLOX has already removed the player’s leaderstats folder. Are there any errors? Oh, and last thing I checked DataStore doesn’t like to save lua arrays so maybe you should import HTTPService and encode it into JSON and save the JSON string.
--So, in total, in order to JSON Encode your table, you would
DataStore:SetAsync(key,game:GetService("HTTPService"):JSONEncode(TableToSave))
local ReplicatedStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(Plr)
local data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Plr
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 1
Level.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = 0
XP.Parent = leaderstats
local Rank = Instance.new("StringValue")
Rank.Name = "Rank"
Rank.Value = "Genin"
Rank.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 10
Coins.Parent = leaderstats
local FC = Instance.new("IntValue")
FC.Name = "FC"
FC.Value = 100
FC.Parent = leaderstats
local key = "user-" .. Plr.userId
local storeditems = data
if storeditems then
Level.Value = storeditems[1]
Rank.Value = storeditems[2]
XP.Value = storeditems[3]
Coins.Value = storeditems[4]
FC.Value = storeditems[5]
else
local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
datastore:SetAsync(key, items)
end
while wait() do
wait(.01)
if XP.Value >= (100 * (Level.Value + 1)) then
Level.Value = Level.Value + 1
XP.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(Plr)
end
end
end)
game.ReplicatedStorage.AddXP.OnServerEvent:Connect(function(plr)
plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 50
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)