Weird Leaderstat Bug

Hi I am currently working on a data save for my game. I have this really weird problem were the data saves yet switches leader stat like this
Screenshot 2020-10-06 174112
Then when I rejoin
Screenshot 2020-10-06 173703

Here is the leaderstat script

	local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	
	local tableToSave = {
		player.leaderstats.Levels.Value; -- First value from the table
		player.leaderstats.Exp.Value -- Second value from the table
	}
	
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Levels = Instance.new("IntValue")
	Levels.Name = "Levels"
	Levels.Parent = leaderstats
	
	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	Exp.Parent = leaderstats 
	

	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
		
	end)
	
	if success then -- If there were no errors and player loaded the data
		Levels.Value = data[1]
		Exp.Value = data[2] -- Set the money to the first value of the table (data)
		 -- Set the coins to the second value of the table (data)
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)
	
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)
		
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Thanks for you help

Try to change it like this


	local tableToSave = {
		[1] = player.leaderstats.Levels.Value, -- First value from the table
		[2] = player.leaderstats.Exp.Value -- Second value from the table
	}

and then

Levels.Value = data[1]
Exp.Value = data[2]
1 Like

How about you do this:

local tableToSave = {
Levels = player.leaderstats.Levels.Value;
Exp = player.leaderstats.Exp.Value
}

1 Like

Change it to…

local success, err = pcall(saveData(player))
1 Like

Try in pairs inside data table, and see difference index and value. (Debugging)

1 Like
local tableToSave = {
		 Levels = player.leaderstats.Levels.Value; -- First value from the table
		Exp = player.leaderstats.Exp.Value;
}

Change

Levels.Value = data[1]
Exp.Value = data[2] -- Set the money to the first value of the table (data)

to

Levels.Value = data["Levels"]
Exp.Value = data["Exp"]
3 Likes