Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I save this in a datastore?

Asked by 5 years ago

I have been trying for the last few hours or so trying to turn the code below into a datastore (well make one for it), I want the gui to know if the user has already purchased the car or not when they join and if so , let them spawn in it. If anyone could help me out it would be really appreciated. Thanks

Code:

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 it 
    local PriceOfItem = game.ReplicatedStorage:WaitForChild("CheckPrice"):InvokeServer(script.Parent.Parent.Name)

    if game.Players.LocalPlayer.leaderstats.Cash.Value >= PriceOfItem and script.Parent.Parent.Purchased.Value == false then
        -- The player has enough cash
        game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - PriceOfItem
        script.Parent.Parent.Purchased.Value = true
        script.Parent.Text = "Spawn"
    end
end

end)

0
You're letting the client determine if they have enough cash? User#25115 0 — 5y

2 answers

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
5 years ago
Edited 5 years ago

----------------------------------------------------- BUYING/CHECKING HANDLER[LOCAL SCRIPT] ----------------------------------------------------- local Player = game:GetService("Players").LocalPlayer local Cars = Player.Cars -- Folder Inside Of Player Containing BoolValues of Cars to check if bought local Stats = Player.leaderstats local Cash = Stats.Cash local SpawnCar = game.ReplicatedStorage.SpawnCar local BoughtFunc = game.ReplicatedStorage.Bought -- Remote Function local PriceOfItem = game.ReplicatedStorage:WaitForChild("CheckPrice"):InvokeServer(script.Parent.Parent.Name) script.Parent.MouseButton1Click:Connect(function() if Cars[script.Parent.Parent.Purchased.Name].Value == true then -- Spawn it SpawnCar:FireServer(script.Parent.Parent.Name) else if Cash.Value >= PriceOfItem then BoughtFunc:InvokeServer(script.Parent.Parent.Name,PriceOfItem) end end end) ----------------------------------------------------- REMOTE HANDLER[GLOBAL SCRIPT] ----------------------------------------------------- local SpawnCar = game.ReplicatedStorage.SpawnCar -- Remove Event local BoughtFunc = game.ReplicatedStorage.Bought -- Remote Function local CheckPrice = game.ReplicatedStorage.CheckPrice -- Remote Function SpawnCar.OnServerEvent:Connect(function(CarName) -- Spawn The Car end) BoughtFunc.OnServerInvoke = function(Player,CarName,PriceOfItem) local Cars = Player.Cars local Car = Cars[CarName] local Stats = Player.leaderstats local Cash = Stats.Cash Cash.Value = Cash.Value - PriceOfItem -- Spawn The Car end CheckPrice.OnServerInvoke = function(Player,CarName) local AllCars = game.ReplicatedStorage.Cars local Car = AllCars[CarName] local Price = Car.Price return Price.Value end ----------------------------------------------------- CAR DATA[GLOBAL SCRIPT] ----------------------------------------------------- local DataStoreService = game:GetService("DataStoreService") local DataStorage = DataStoreService:GetDataStore('CARDATASTORAGE') local DataKey = "~~" local CarsInGame = game.ReplicatedStorage.Cars -- A Folder Where All The Cars That Exist In The Game Are game.Players.PlayerAdded:Connect(function(player) local PlayerData = DataStorage:GetAsync(DataKey..player.userId) -- Players Existing Data If They Have Played Before if PlayerData then for i,v in pairs(CarsInGame:GetChildren()) do -- Will Create BoolValues For Each Car So It Can Determine If You Have Bought A Car local Bool = Instance.new("BoolValue") Bool.Name = v.Name Bool.Value = PlayerData[i] Bool.Parent = player.Cars end else for i,v in pairs(CarsInGame:GetChildren()) do -- Will Create BoolValues For Each Car So It Can Determine If You Have Bought A Car local Bool = Instance.new("BoolValue") Bool.Name = v.Name Bool.Value = false Bool.Parent = player.Cars end end end) game.Players.PlayerRemoving:Connect(function(player) local Cars = player.Cars local DataTable = {} for i,v in pairs(Cars:GetChildren()) do table.insert(DataTable,v.Value) end DataStorage:SetAsync(DataKey..player.userId) -- Saves All Cars If They Are Owned Or Not end)
0
I made this quick without testing so idk if it will work exactly. LuaDLL 253 — 5y
0
Thanks , got some errors (think I put it in the wrong place) but you have answered my question overall:) I sort of get it now Enzo341 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You should have a boolValue named Car or something. When the player buys the car you cans set the value of it to true. Using datastores, you can save this value, letting the game know if they bought it by checking if the value is set to true.

DataStore information

Saving a value tutorial

0
Thank you , I will go and try it out. Enzo341 4 — 5y

Answer this question