Normal datastore is bugging and messing values, how do I fix it?

So I have this code right here for a cookie clicker type game, it works and all but the values get wierd and messy each time i playtest, like the multiplier randomly becomes 515845 or the intvalues switch with one and another… Is anyone else facing this issue?

--Services

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("x65d8areno")

--Events
game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local cookies = Instance.new("IntValue", leaderstats)
	cookies.Name = "Cookies"
	cookies.Value = 0
	
	local multiplier = Instance.new("IntValue", leaderstats)
	multiplier.Value = 1
	multiplier.Name = "Multiplier"
	
	local data
	local suc, err = pcall(function()
		data = datastore:GetAsync("-key"..player.UserId)
	end)
	if suc then
		if data then
			cookies.Value = data[1]
			multiplier.Value = data[2]
			print("Setted")
		end
	else
		warn(err)
	end
end)
--Function
local function savedata(player)
	local savetable = {
		player.leaderstats.Multiplier.Value,
		player.leaderstats.Cookies.Value
	}
	
	local suc, err = pcall(function()
		datastore:SetAsync("-key"..player.UserId, savetable)
	end)
	if suc then
		print("Saved")
	else
		warn(err)
	end
end
--Events
game:GetService("Players").PlayerRemoving:Connect(savedata)
--GameShutDown
game:BindToClose(function()
	for i, player in pairs(game:GetService("Players"):GetChildren()) do
		savedata(player)
	end
end)

NOTE: the script does save the player data, but sometimes it just messes the values between multiplier and cookies.

local savetable = {
	player.leaderstats.Multiplier.Value,
	player.leaderstats.Cookies.Value
}
cookies.Value = data[1]
multiplier.Value = data[2]

Might be worth swapping somewhere? you just confused the values

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