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

How would I make a datastore script for saving cars that you've bought?

Hi, I need help with a datastore script that saves the cars you've bought. I've been trying and trying but I can't seem to figure it out. The way the car spawns is by a gui button. The car spawns fine and the buying event of the car works fine.

I followed AlvinBloxs' video on how to make a car spawner gui: https://www.youtube.com/watch?v=zM6QhIn_JBY

In ReplicatedStorage there is a Remote Function named "CheckPrice" and Remote Event named "SpawnCar".

The cars are in ServerStorage in a folder named "Cars". In the folder are 2 cars, one car named "Car" and another car named "2008 Honda CR-V". In each car is a IntValue named "Price". Each IntValue has a value of 1000.

Also in StarterGui is a screen gui named "PurchaseGUI". In it is a frame named "Frame" and in it are 2 image labels which have the same name of the cars(So one image label named "Car" and another named "2008 Honda CR-V"). In each ImageLabel there is a Text Button named "BuyButton". In each buy button there is a BoolValue named "Purchased" and a LocalScript named "LocalScript". In the LocalScript it says:

script.Parent.MouseButton1Click:Connect(function()
	
	if script.Parent.Purchased.Value == true then
		game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)
	else
		local PriceOfItem = game.ReplicatedStorage:WaitForChild("CheckPrice"):InvokeServer(script.Parent.Parent.Name)
		
		if game.Players.LocalPlayer.leaderstats.Money.Value >= PriceOfItem and script.Parent.Purchased.Value == false then
			
			game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - PriceOfItem
			print("Car was bought")
			script.Parent.Purchased.Value = true
		end
	end
end)

The leader stats and the spawn vehicle script are in the same script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local money = Instance.new("IntValue")
	money.Name = "Money" -- The name that shows at the top change if you want
	money.Value = 10000 -- How much to start with
	money.Parent = leaderstats
	
end)


game.ReplicatedStorage:WaitForChild("CheckPrice").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
	if game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()then
		game.ServerStorage.Cars.Car.Value = true
	end
end)

If you need more information please notify me.

1 answer

You can make a table for every car that you've bought. Eg:

Script 1:

car.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		remoteEvent:FireClient(player)
	end
end)

Script 2:

remoteEvent.OnClientEvent:Connect(function()
	table.insert(table, value)
end)

When the player leaves the game, you can save the table. You can also use the table to check if the player owns any cars.

1 comment

0

Where do I put the script and what would the full script be with the cars?

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