Overwriting all player's data instead of just one?

Introduction

I’ve been working on customization for Drifting Simulator lately, and so I’ve run into a confusing problem. Or I’m just sleepy :laughing:! Anyways, I use a ModuleScript which contains different functions which can be called through any script which require()s the module to save, load, update the cached data or get data.


Where’s the code?

I execute this code when a player leaves a customization garage:
(Where functions is the saving module, )

local newCarData = {Name = driveSeat.Parent.Name} --All car data tables have the car name stored inside
local values = {} --The instances like IntValues which need to be saved

for i, v in pairs(functions.default_data) do
	local val = customizationFolder:FindFirstChild(i)
	if val then
		table.insert(values, val)
		--[[Here we insert all the values which are supposed to be saved into the "values" table,
		"default_data" is a table with the names of all the values which are supposed to be saved.]]
	end
end

--In this loop we feed data into the table we're gonna be saving.
for _, v in pairs(values) do
	if v:IsA("Color3Value") then
		newCarData[v.Name] = {r = v.Value.R, g = v.Value.G, b = v.Value.B}
	else
		newCarData[v.Name] = v.Value
	end
end

--In this loop we check if data already exists for the same car. If there is we delete it.
for i, dataTable in pairs(pData) do
	if type(dataTable) == "table" then
		if dataTable.Name == newCarData.Name then
			table.remove(pData, i)
		end
	end
end
						
--We insert the table into the table which contains tables for other cars. LOL
table.insert(pData, newCarData)

--We call the "update_cached" function.
functions.update_cached(plr, pData)

This is what the update_cached function does:

cached_playerdata[tostring(plr.UserId)] = newData

What’s the cached_playerdata table? Well, when any script calls the get_playerdata function, the function returns either the player’s cached_data, or an empty table if this does not exist. This will always exist if the player has save data as this is set to when the player joins and the data is first loaded.


What exactly is the issue?

Well, the problem is that whenever someone leaves a garage, all other players have the same data for the car they just tuned. Let me clarify that:

  • Player 1 enters the garage with a Mustang.
  • Player 2 is on the other side of the map, driving his Supra.
  • Player 1 leaves garage after tuning it, let’s say he applied a blue paint and -10 camber.
  • Player 2 after a while decides he wants to drive his Mustang now. He spawns it and - his Mustang is now also blue with -10 camber!

Conclusion

I’ve been working on these systems for quite a few hours and by now I’m sleepy and braindead lol. I’d really appreciate it if anyone could figure out what I’m doing wrong or point me in the right direction! Ask me if you need any more information, I’ll be happy to provide it.


Edit:

Just for some extra clarification, here’s basically what the “default_data” table is.

functions.default_data = {
	FCamber = 0,
	RCamber = 0,
	PrimaryColor = {r = 255, g = 255, b = 255},
	EngineLevel = "Stock",
}

I am sorry if I am blind once again but what is the scripts parent?

LocalScript which fires the RemoteEvent is in a GUI in PlayerGui, the module is parented to the Script which listens for the RemoteEvent, which itself is inside ServerScriptService.

1 Like

I believe your problem is related to your default data. Whenever you get your default data you’re not doing a deep copy. That means the color table stored in your default data is always the same exact table. Since you store this same table for different player’s cars setting r, g, or b of the table appears to update for all other players as well since it’s really just the same exact table.

1 Like

By the way do any errors pop up?

Nope, it’s more of a logical error.

That’s possible; I’ll try changing some stuff and I’ll let you know what happens :+1:

1 Like

I thought about this and I deemed this to be a likely issue. I don’t really have the time to do much so instead of just the car’s name I set the “Name” key’s value to be (UserId)_(CarName) and it worked! I’ll mark your answer as a solution as this was the cause. (At least as far as I’ve been able to test)

1 Like