You can fix this with a simple if check.
@ vxcaie
local function getPlayerData(player)
local data = {} -- Initialize the data table -- think this is the problem need this Initialized
local success, errormessage = pcall(function()
local keys = {}
for _, key in ipairs(CAR_OWNERSHIP_KEYS) do
keys[key] = player.UserId.."-"..key
end
keys.credits = player.UserId.."-credits"
data = myDataStore:GetAsync(keys)
end)
return success, data, errormessage
end
Is there a difference between the scripts?
Change this to
if success and data then
for key, value in pairs(data) do
local valueObject = leaderstats:FindFirstChild(key)
if valueObject then
valueObject.Value = value
end
end
elseif not success and not data then
print("There was an error while getting "..player.Name.."'s data")
warn(errormessage)
end
Sorry my bad I edited the code should be fixed now
How should the code be fixed??
Itās more than hard to show a code fix without having the full program in a case like this. Iām not willing to create this code myself to find a simple syntax error. Iāll try to give you a good logic approach, but youāll need to hammer it out. Youāre main problem here was the over use of GetAsync and SetAsync. Try to build your table and save it once. And read your table once. Even if that means reading it to a temp table to then work off of.
I tried to use tables for saving, it kinda worked: it saves the credits but when I buy the car the values stays at the value before buying the car, thus having to buy the car againā¦
I edited the code you can just implement it
I used the older version of the script (the one with the start, slightly modified) but I updated it to the newer one and now nothing weirdly savesā¦