I have no idea why this isn't working. My currency doesn't load up in my leaderstats, and there is no error in the dev console. This is my script:
local currencyName = "Cash"
local dss = game:GetService("DataStoreService")
local datastore = dss:GetDataStore("CashDataStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local cash = Instance.new("IntValue",folder)
cash.Name = currencyName
local ID = currencyName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = datastore:GetAsync(ID)
end)
if savedData ~= nil then
cash.Value = savedData
print("Data loaded")
else
cash.Value = 1200
print("New player to the game")
end
game.ReplicatedStorage.ChangeValues.Changebmw.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchasedbmw.Value = 1
end)
game.ReplicatedStorage.BuyCars.BuyBmw.OnServerEvent:Connect(function()
local priceofitem = 1000
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchasedbmw.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedbmw.Value = 1
end
end)
game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(oneplayer, NameOfCar)
local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
car:SetPrimaryPartCFrame(oneplayer.Character.HumanoidRootPart.CFrame)
car.Parent = workspace
car:MakeJoints()
car.Name = player.name.."'s "..NameOfCar
car.Wheels.RR.ChildAdded:Connect(function(child)
if child.Name == "Smoke" then
repeat
wait(0.001)
if child.Rate ~= 0 then
cash.Value = cash.Value + 10
wait(1)
end
until car == nil
end
end)
end)
game.ReplicatedStorage.BuyCars.BuyGtr.OnServerEvent:Connect(function()
local priceofitem = 0
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchasedgtr.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedgtr.Value = 1
end
end)
game.ReplicatedStorage.BuyCars.BuyHoon.OnServerEvent:Connect(function()
local priceofitem = 750
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchasedbhoon.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedhoon.Value = 1
end
end)
game.ReplicatedStorage.BuyCars.BuyMc.OnServerEvent:Connect(function()
local priceofitem = 500
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchasedmc.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedmc.Value = 1
end
end)
game.ReplicatedStorage.BuyCars.BuyS13.OnServerEvent:Connect(function()
local priceofitem = 10000
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchaseds13.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedbs13.Value = 1
end
end)
game.ReplicatedStorage.BuyCars.BuyPor.OnServerEvent:Connect(function()
local priceofitem = 200
if cash.Value >= priceofitem and game.Workspace.PurchasedValues.Purchasedpor.Value == 0 then
cash.Value = cash.Value - priceofitem
game.Workspace.PurchasedValues.Purchasedpor.Value = 1
end
end)
game.ReplicatedStorage.ChangeValues.Changehoon.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchasedhoon.Value = 1
end)
game.ReplicatedStorage.ChangeValues.Changemc.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchasedbmc.Value = 1
end)
game.ReplicatedStorage.ChangeValues.Changegtr.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchasedgtr.Value = 1
end)
game.ReplicatedStorage.ChangeValues.Changes13.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchaseds13.Value = 1
end)
game.ReplicatedStorage.ChangeValues.Changepor.OnServerEvent:Connect(function()
game.Workspace.PurchasedValues.Purchasedpor.Value = 1
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
datastore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

2 comments
Make sure that API services are enabled, I see nothing wrong with your script. Also make sure to place the script inside ServerScriptService if you haven't.
Yep, that was the problem. Thank you.