How would I script this

So I am trying to script something where if you press a button then the button takes away 500 bucks an then it spawns a car(Thats scripted) But I am trying to find out how would I save it were when I join a new server it says your already own the car and lets you spawn it in?

datastores

My recommendation and how I would go about this is datastoring a table. Simply set up a datastore or use an existing one and save a table…

local purchasedVehicles = {}

--// Imagine this is your code of someone buying a vehicle
script.Parent.MouseButton1Click:Connect(function()
   leaderstats.Money.Value = leaderstats.Money.Value - 500
   table.insert(purchasedVehicles, vehicle)
end)

--// Save the table
datastore:SetAsync(purchasedVehicles)
1 Like

Alright Ill try it. Ill let you know if I need help.

not really a good scripter myself, but I think you forgot to add an if statement to check if the player has at least 500 money!

“Imagine this is your code of someone buying a vehicle” I wasn’t giving them the actual code for buying the vehicle I was just trying to show them somewhat of how to use it

2 Likes

Fair enough. It’s just the guy seems very inexperienced and you could at least do a bit more hand-holding since you’re still helping him

I know how to do that I just never know how to use datastores

He said he already did that part

1 Like

Question, How would I get the vehicles the owner has and let them be able to spawn it in?

local ds = game:GetService('DataStoreService'):GetDataStore('whatYourDatastoreIsNamed')
local ownedVehicles = {}

--// Adding onto what was said earlier about how to get them
--// After the game is loaded ownedVehicles will have all the vehicles
--// You just simply have to look through the table and if you find a vehicle set it to owned

game.Players.PlayerAdded:Connect(function(plr)
   local saveCars = ds:GetAsync(plr.UserId)

   if saveCars ~= nil then
      ownedVehicles = saveCars
   else
      ownedVehicles = {}
   end
end)

--//Buying the vehicle
script.Parent.MouseButton1Click:Connect(function()
   if leaderstats.Money.Value < 500 then return; end
   leaderstats.Money.Value = leaderstats.Money.Value - 500
   table.insert(ownedVehicles , vehicle)
   ds:SetAsync(plr.UserId, ownedVehicles)
end)

game.Players.PlayerRemoving:Connect(function(plr)
   local Success, Error

   Success, Error = pcall(function()
      ds:SetAsync(plr.UserId, ownedVehicles)
   end)

   if Success then
      print(plr.UserId..' : Saved Data')
   else
      warn(Error)
   end
end)

1 Like

I added it to attempt a save every time you buy a vehicle already :slight_smile:

1 Like

Is this ment to be local script or a normal script?

1 Like

blah blah blah waste a bunch of space: Normal Script

1 Like

If I wanted to find it in a table would I do something like this:

for _,v in ipairs(Table) do
if v ~= ItemName then
 -- If owned then let they spawn
else
-- Make them buy it
   end
end 
--// Say the car name is saved in the table as "slowCarOne"

local ownedCars = {slow, fast, carOne, slowCarOne, slowCarTwo}

if table.find(ownedCars, slowCarOne) then
   print('true')
else
   print('false')
end

--// OUTPUT WOULD BE "true"

Now if we didnt have it inside the table

--// Say the car name is saved in the table as "slowCarOne"

local ownedCars = {slow, fast, carOne, slowCarTwentyOne, slowCarTwo}

if table.find(ownedCars, slowCarOne) then
   print('true')
else
   print('false')
end

--// OUTPUT WOULD BE "false"
1 Like
if table.find(ownedVehicles, vehicle) then
		print('true')
	else
		print('false')
		table.insert(ownedVehicles , vehicle)
		ds:SetAsync(plr.UserId, ownedVehicles)
	end 

attempt to index nil with ‘UserId’ (Error)

If you have Discord, I would prefer better to have a conversation over there if not, I can continue here it’s just I don’t have notifications on here and I don’t want to ghost you. If you’re down drop it in my DMs.

But your issue is

plr is not defined

ii_inforcelaw#3383 Thats my dizzy if you want to chat though it

To summarize what happened inside of the Discord DM’s for those who want to know something revolving around the same issue:

local ds = game:GetService('DataStoreService'):GetDataStore(game.PlaceId..'dataStoreName')
local ownedVehicles = {}

--// Adding onto what was said earlier about how to get them
--// After the game is loaded ownedVehicles will have all the vehicles
--// You just simply have to look through the table and if you find a vehicle set it to owned

local function spawnCar(vehicle)
    --// Do your car spawning here
end


game.Players.PlayerAdded:Connect(function(plr)
        print(plr.Name)
    local saveCars = ds:GetAsync(plr.UserId)

    if saveCars ~= nil then
        ownedVehicles = saveCars
        warn(ownedVehicles)
    else
        ownedVehicles = {}
        warn(ownedVehicles)
    end
    
    spawn(function() --// Simply making a function to save every 5 minutes
        while wait(300) do
            local Success, Error

            Success, Error = pcall(function()
                ds:SetAsync(plr.UserId, ownedVehicles)
            end)

            if Success then
                warn(ownedVehicles)
                print(plr.UserId..' : Saved Data')
            else
                error(Error)
            end
        end
    end)
    
end)

--//Buying the vehicle
game:GetService('ReplicatedStorage'):FindFirstChild('purchaseVehicle').OnServerEvent:Connect(function(plr, vehicle)
    --// I would check if the player has enough money again here, Never trust the client.
    --/ I would also search to see if the vehicle actually exists, if not then return :)
    if table.find(ownedVehicles, vehicle) then spawnCar(vehicle) return; end --/ Only lets them buy it once ofc
    --/ Remove the money
    table.insert(ownedVehicles, vehicle)
    
    spawnCar(vehicle)
    
    local Success, Error

    Success, Error = pcall(function()
        ds:SetAsync(plr.UserId, ownedVehicles)
    end)

    if Success then
        warn(ownedVehicles)
        print(plr.UserId..' : Saved Data')
    else
        error(Error)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local Success, Error

    Success, Error = pcall(function()
        ds:SetAsync(plr.UserId, ownedVehicles)
    end)

    if Success then
        warn(ownedVehicles)
        warn(plr.Name..', has left the game... Saved User: '..plr.Name..':'..plr.UserId.."'s Data")
    else
        error(Error)
    end
end)

Simply what I did here was create a simple and basic Datastore for them and saved a table with all the cars in it to it: When the player joined I loaded the table and set it to the pre-existing table that way they could further use it…

I also added a save interval of 5 minutes and also a save for whenever they purchase a vehicle to help further prevent dataloss. If you have any questions feel free to DM me if you are having the same issues.

1 Like