local currencyName = "Money"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local money = Instance.new("IntValue")
money.Name = currencyName
money.Parent = folder
local ID = currencyName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
money.Value = savedData
print("Data Loaded")
else
--NewPlayer
money.Value = 100000
print("New Player to the game")
end
end)
--//Video #2: Start\\--
game.ReplicatedStorage:WaitForChild("Check_Price").OnServerInvoke = function(player,NameOfCar)
return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
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)
--//Video #2: Finish\\--
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
print("Saved")
end)
game:BindToClose(function()
--When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(5)
end)
The code for the local script is:
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Purchased.Value == true then
--//Spawn It\\--
game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)
else
--//Let Them Purchase Car\\--
local PriceOfItem = game.ReplicatedStorage:WaitForChild("Check_Price"):InvokeServer(script.Parent.Parent.Name)
if game.Players.LocalPlayer.leaderstats.Money.Value >= PriceOfItem and script.Parent.Parent.Purchased.Value == false then
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - PriceOfItem
script.Parent.Parent.Purchased.Value = true
end
end
end)
