Combined data store scripts not working

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.

1 Like

oh sorry theres a option… i miss reading api

1 Like

It is already enabled. theres other way to solve this?

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.

1 Like

i cant understand the last thing, so there are no errors…
i havent learned about HTTPService or JSON yet, can you put a script as Answer, please?

game:GetService(‘HTTPService’):JSONEncode(YourTableHere)

1 Like

Table is the name of datastore?

So, in total, in order to JSON Encode your table, you would
DataStore:SetAsync(key,game:GetService(‘HTTPService’):JSONEncode(TableToSave))

In order to access your datastore, you would
local data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

1 Like

like this?

--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)

Lemme fetch something for you, wait a sec

1 Like
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 = game:GetService(‘HTTPService’):JSONDecode(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,game:GetService("HTTPService"):JSONEncode(items))
end)
1 Like

will test it :smiley:

if you play my game someday you will have special perks

1 Like

its like the leaderstats wasnt created. I tested first on Player Launcher and no leaderstats was created, will check roblox studio output

So, there is the output:

15:56:11.657 - ServerScriptService.leaderstats:40: Expected identifier when parsing expression, got ‘local’

there is something wrong with line 40

Sorry for the late reply, edited my message. Should be fixed now.

1 Like

When saving arrays to the DataStore, Roblox automatically encodes the data into JSON.

1 Like