Cash and BoolValues not saving in DataStore2 (Roblox)

Published on December 11, 2019

 --Variables
local DataStore2 = require(game.ServerScriptService.DataModule)
local MainKey = "MainKey"
DataStore2.Combine(MainKey,"Stats","Cars")


--Data Table
 local function SetDataTable()
local UserData = {
    Stats = {
        ["Cash"] = 50000000;

    };
    Cars = {
        ["Hyperno"] = false;
        ["Explorus"] = false;
        ["Trofeo"] = false;
    };
}
return UserData -- get datatable
end

game.Players.PlayerAdded:Connect(function(plr)
local UserData = DataStore2(MainKey,plr):Get(SetDataTable())

local leaderstats = Instance.new("Folder") 
leaderstats.Name = "leaderstats"

local achfolder = Instance.new("Folder")
achfolder.Name = "CarFolder"

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"

local Hyperno = Instance.new("BoolValue")
Hyperno.Name = "Hyperno"
local Explorus = Instance.new("BoolValue")
Explorus.Name = "Explorus"
local Trofeo = Instance.new("BoolValue")
Trofeo.Name = "Trofeo"

local StatsData = DataStore2("Stats",plr)
local CarsData = DataStore2("Cars",plr)

local function UpdateStats(UpdatedValue)
    Cash.Value = StatsData:Get(UpdatedValue).Cash

end

StatsData:OnUpdate(UpdateStats)
local function UpdateCar(UpdatedValue)
    Hyperno.Value = CarsData:Get(UpdatedValue).Hyperno
    Explorus.Value = CarsData:Get(UpdatedValue).Explorus
    Trofeo.Value = CarsData:Get(UpdatedValue).Trofeo
end

UpdateStats(UserData.Stats)
UpdateCar(UserData.Cars)

StatsData:OnUpdate(UpdateStats)
CarsData:OnUpdate(UpdateCar)


leaderstats.Parent = plr
achfolder.Parent = plr
Cash.Parent = leaderstats
Hyperno.Parent = achfolder
Explorus.Parent = achfolder
Trofeo.Parent = achfolder

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)

return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end

game.ReplicatedStorage:WaitForChild("CheckPurchase").OnServerInvoke = function(player,NameOfCar)

return game.Players:FindFirstChild(player.Name).CarFolder:FindFirstChild(NameOfCar).Value
end


end)


 game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
car.Parent = workspace
car:MakeJoints()
car.Name = player.Name.."'s"..NameOfCar
end
)

game.ReplicatedStorage:WaitForChild("UnlockCar").OnServerInvoke = function(player,NameOfCar)
if game.Players:FindFirstChild(player.Name).CarFolder:FindFirstChild(NameOfCar).Value == false then
    print (game.Players:FindFirstChild(player.Name).CarFolder:FindFirstChild(NameOfCar))
        game.Players:FindFirstChild(player.Name).CarFolder:FindFirstChild(NameOfCar).Value = true
        return game.Players:FindFirstChild(player.Name).CarFolder:FindFirstChild(NameOfCar).Value
        end
 end

I’m trying to use DataStores2 in order to make it save the boolvalues and Cash values. The purchase script works perfectly however the main script which is the one I’m showing you is not saving the cash nor the bool value determining if the player has the car.

How can I fix this?

I’ve tried looking at tutorials and the main tutorial I followed is https://www.youtube.com/watch?v=hmRBvZD1pRw.

Most of the code is based on his code. What did I do wrong? And what can I do to make it actually save?

Enjoyed this video?
Cash and BoolValues not saving in DataStore2 (Roblox) 2 – Redblox Games
"No Thanks. Please Close This Box!"