Whole list of player data updating instead of just a specific player data key

I have a variable LoadedData that is a list of all the players data within a server inside of my DataStore script. I’m trying to call a function which updates a players data given a set a parameters, but for some reason the script is changing everyone’s data instead of the just a single players data thate I specified in the ‘player’ parameter.

local function UpdateData(player, Action, ChangedData, NewData)
	if Action == nil then return end
	
	local playerData = LoadedData[tostring(player.UserId)]
	if not playerData then return end
	
	if not playerData[ChangedData] then return end
	
	--Set Previous Loaded Data to new Loaded Data--
	LoadedData[tostring(player.UserId)][ChangedData] = NewData -- This line changes the data
	UpdateEvent:Fire(LoadedData)
end

I have no clue why this happens, and I’ve tried to change many things to see if it would fix it, but there has been no luck. I can provide more code if it is needed.

2 Likes

What have you done so far in terms of solving the issue?

2 Likes

It’s always a good idea to watch how your variables change if you have an issue. Try using debug statements print(var) to see which values are what at what time. See what the values of player and ChangedData are - it could lead to an insight

5 Likes

Sorry for the late response, I completely forgot about this dev forum post after I fixed the issue roughly 6 hours later.

I did put print statements everywhere to find the exact problem, and I came to the conclusion that the [ChangedData] in LoadedData[tostring(player.UserId)][ChangedData] was oddly changing all of the players data rather than just the key that I asked for. In the end, I just revamped the functionality of the UpdateData function to modify the entirety of the playerData rather than just a single piece of it.

I still have no clue why that single piece of text was messing everything up, so I’ll try finding a solution for that later so that I don’t encounter it again. I’ll mark your comment as the solution since that is basically what I did to solve the issue.

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