Previous data is overwriting current data

local Emotes = {
	["Rock On"] = {false,false,"http://www.roblox.com/asset/?id=17694308547"},
	["Clap"] = {false,false,"http://www.roblox.com/asset/?id=17696748004"}
}

I’m working on saving an emotes system.

The problem is I’m continuing to create emotes but when I add them to the list, they don’t go to the players sessionData when every is saved using GetAsync when the player joins. The Datastore doesn’t recognize the key so it can’t be a part of the sessionData[player][“Emotes”].

local function Emotes(tab_)
	tab_ = {}
	for emote,value in pairs(EmotesList) do
		tab_[emote] = value
	end
	return tab_
end

Players.PlayerAdded:Connect(function(player)
	local key = tostring(player.UserId)
	
	sessionData[player] = {}
	
	sessionData[player]["Money"] = 0
	sessionData[player]["Equipped"] = ""
	sessionData[player]["Abilities"] = Abilities(sessionData[player]["Abilities"])
	sessionData[player]["Kills"] = 0
	sessionData[player]["Wins"] = 0
	
	sessionData[player]["Level"] = 1
	sessionData[player]["Experience"] = 0
	
	sessionData[player]["Emotes"] = Emotes(sessionData[player]["Emotes"])
	sessionData[player]["EmoteProgress"] = 2
	
	sessionData[player]["Gamepasses"] = {
		["DoubleEmotes"] = false
	}
	
	local success,result = pcall(function()
		return DataStore:GetAsync(key)
	end)
	if success and result then
		for i,v in pairs(result) do
			print(i,v)
			sessionData[player][i] = v
		end
	end
	print(sessionData[player]["Emotes"])
	leaderstats(player)
	data(player)
	task.spawn(function()
		while true do
			if DataStore:GetAsync(key) then
				Leaderboards:Set(player,"Kills",sessionData[player]["Kills"])
				Leaderboards:Set(player,"Wins",sessionData[player]["Wins"])
			end
			task.wait(100)
		end
	end)
	
	Leveling:Init(player)
end)

I’m iterating through the sessionData and changing key to value so I don’t know how it would override a new key. Any ideas?

Initialize default emotes, then merge saved emotes from the datastore to ensure new emotes are correctly added.

Thats what I did by creating the 2 emotes in the list and then adding/merging them into the datastore. The Clap emote never appears though. It is the 2nd emote I created.

local function deepCopy(tab_)
	local copy = {}
	for k, v in pairs(tab_) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end

local function Emotes(tab_)
	tab_ = {}
	for emote,value in pairs(EmotesList) do
		tab_[emote] = value
	end
	deepCopy(tab_)
	return tab_
end

I fixed the script, definitely didn’t use AI.

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