Something wrong with DataStore

So THECOOLBOSS99999 helped me to save the datastore but theres something wrong with line 40 but i didnt know how to solve this.

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

script:

-- BY ClanShad27 and THECOOLBOSS99999


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 = local data = 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)
3 Likes

What line in your script is the error on?

1 Like

the output says theres an error on line 40

Is it when it says Local Data = …

1 Like

I meant the actual line in your code snippet. The code isn’t numbered by line, so a little marker to mark which line is line 40 would be helpful.

1 Like

It’s just above the IF statements, where he tried to use local twice on one single line.

1 Like

local storeditems = local data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

You used local while defining a variable. You’ll need to have it on a separate line to define two different variables.

2 Likes

what part of this line i need to erase?

Just type Local Data on top and add Data = …

1 Like

then where will be stored items?

So like this:
local data
local storeditems = data = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

As @Polyheximal said, you can’t use local when defining a variable on the same line, you’ll have to add seperate lines.

1 Like

so the second ‘=’ is wrong, this is what the code says

Not 100% sure what you’re trying to do but:
local storeditems = game:GetService(‘HTTPService’):JSONDecode(DataStore:GetAsync(key))

You can’t assign values to variables like the way @Tamzy3D suggested.

2 Likes

is there a reason you are encoding data in the first place? this is automatically done internally, also at some point you might what to add in some pcalls.

3 Likes

so i dont know how to do this, i know a little bit of programming. so i have do what Polyheximal said then this appeared on output:

16:18:25.034 - ServerScriptService.leaderstats:40: Unexpected Unicode character: U+2018. Did you mean ‘’'?

The (‘HttpService’) probably has to be (“HttpService”), so 2 speech marks. But since they are both the same, I’m not sure.

2 Likes
local storeditems = game:GetService("HttpService"):JSONDecode(DataStore:GetAsync(key))

in this line, the term ‘DataStore’ are Unknow global…

You should make your own datastore.

Local DataStoreService = game:GetService(“DataStoreService”)

Local DataStore = DataStoreService:GetDataStore(“DataStore”)

1 Like

Like this?:

-- BY ClanShad27 and THECOOLBOSS99999
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

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