Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I save this in a datastore?

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)

1 comment

0

You're letting the client determine if they have enough cash?

2 answers

✓ Accepted answer

----------------------------------------------------- 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)


2 comments

0

I made this quick without testing so idk if it will work exactly.

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

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

1 comment

0

Thank you , I will go and try it out.

This discussion is preserved as it was. New answers, comments, and votes are closed.