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

How do I allow players to save their in game purchases they buy with the in game currency?

How do I allow players to save their in game purchases they buy with the in game currency.

My Code for purchasing an item with in game currency (cash)

local plr = game.Players.LocalPlayer
local replicatedstorage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:connect(function()
	if plr.Backpack:FindFirstChild("GravityCoil") or plr.StarterGear:FindFirstChild("GravityCoil") or game.Workspace[plr.Name]:FindFirstChild("GravityCoil") then
		script.Parent.Parent.TextLabel.Text = "You have already bought this item!"
		wait(3)
		script.Parent.Parent.TextLabel.Text = "SHOP"
		else
	
	if plr.leaderstats.Cash.Value >= 500000 then
		replicatedstorage.GravityCoil:Clone().Parent = plr.Backpack
		replicatedstorage.GravityCoil:Clone().Parent = plr.StarterGear
	else
	local cashrequired = 500000 - plr.leaderstats.Cash.Value
	script.Parent.Parent.TextLabel.Text = "You need "..cashrequired.." more money to buy this item"
	wait(3)
	script.Parent.Parent.TextLabel.Text = "SHOP"
	end
	
	end
	
	
	
end)


I made this code to save the cash a player has in my game and it works. How would I use it to also save the in game purchases of a player.

My Purchasable Items are in replicatedstorage.

How would I make sure that the player can keep their purchase so they when they rejoin the game they still have the gear they purchased with in game currency.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Cash"
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 0
	end
	
	cash.Changed:connect(function(Val)
		CashStore:SetAsync("Points_"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

Thank You and help is appreciated

7 comments

0

i looked at that but i still didnt understand

0

it doesnt say how to save purchases

1

It isn't going to tell you exactly what to do, it gives you the information you need to "build" the script you would use. Read it through a little more "carefully" and think about how you could use it to save a player's purchases.

0

Hint: Tables!

0

um

0

i found a helpful video which answered my question ill post link.

1 answer

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