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 ! 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",
}