Data clone with new players

i have a problem that the new players data is a clone from old players in the same server
here is the part that i think had the problem:

repeat
		Success, PlayerData = pcall(function()
			return DataBase:GetAsync(Player.UserId)
		end)

		if not Success then
			warn("Error loading data for player: " .. tostring(Player.UserId) .. " | " .. tostring(PlayerData))
			task.wait(2) -- Adding a slight delay before retrying
		end
		Attempt += 1
	until Success or Attempt > 3

	if Success then
		if not PlayerData then
			warn(tostring(DefaultData))
			PlayerData = table.clone(DefaultData)
		else
			-- Ensure missing fields are properly initialized
			for key, defaultValue in pairs(DefaultData) do
				if PlayerData[key] == nil then
					PlayerData[key] = defaultValue
				elseif type(defaultValue) == "table" then
					for subKey, subDefaultValue in pairs(defaultValue) do
						if PlayerData[key][subKey] == nil then
							PlayerData[key][subKey] = subDefaultValue
						end
					end
				end
			end
		end

		-- Assign values to leaderstats
		Level.Value = PlayerData.Stats["Level"]
		StreakWin.Value = PlayerData.Stats["Streak Win"]
		Coins.Value = PlayerData.Stats["Coins"]

		-- Store cloned data to prevent shared references
		Data[Player.UserId] = table.clone(PlayerData)

		-- Increment visit count
		Data[Player.UserId]["Stats"].Visits += 1
	else
		warn("Unable to get data for player: " .. Player.UserId)
		Player:Kick("Unable to load data, please rejoin.")
		return
	end

could you clarify what an “old player” and a “new player” are?
is an “old player” any other player in the same server that joined before the current player, or just the current player that rejoined the same server?

old player is a return player that already have a data saved
a new player is a player join for the first time that have no data

The issue might be that the Level, StreakWin, and Coins variables are unassigned in this portion of the script. Assuming the script runs perfectly fine, it should mean that these variables are assigned somewhere else in the script, possibly from the older player. Are you sure that the variables are assigned to the new player before this portion of the code runs?

	-- Assign values to leaderstats
	Level.Value = PlayerData.Stats["Level"]
	StreakWin.Value = PlayerData.Stats["Streak Win"]
	Coins.Value = PlayerData.Stats["Coins"]

i create a table called DefaultData and freeze it so it has to be good

for those who search a fix for the problem i ended with creating a key in the database called DefaultData and give it to the new players

if not PlayerData then
	DataBase:GetAsync("DefaultData")
else

but you have to create one first you can use this script but use it one time

DataBase:SetAsync("DefaultData", DefaultData)

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