Data storage saving other values except 2

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)

1 Like

please put a pcall statement around the :setAsync()

I’m sorry, but what do you mean “around”?

I think the SetAsync is place incorrectly. You should want to save the values when the player leaves the game, not when they join. So use game:GetService(“Players”).PlayerRemoving and then put the code for saving under it

For some reason, even after doing what you told me, all the values are saving except level and experience.

1 Like

I finally found the issue. I have just found out that ennumeration in this script is improtant, and that in the other script they have to be in the same order as I stated in: 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].

These were not in order. Lol.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.