How to save item like pets , trails , tools if player purchased it

Assuming you’re a newcomer to LUA, and I myself am not a “professional” at all, nor is anyone really, you can always learn new methods when it comes to scripting! DataStores for Models, Tools, Hats, Armours, Trails, etc, are USUALLY saved with things called BooleanValues or BoolValues. And saving them really isn’t as difficult a task as some may believe.

The post above, regarding the DataStore(DS) System that that programmer created is an excellent source, but if you’re new, it can/could be over complicated. I will provide you with a link below of a DevForum post in which I’ve found out to be extremely helpful when I was learning the new way of tool persistence or DataStoring tools.

This is the Tools Link, for saving TOOLS ONLY

Secondly, DataStoring values, boolean values, need to be done via a Table.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local BoolTestDataStore = DataStoreService:GetDataStore("emp44")

local data = {
	--// Tools \\--
	["Sword"] = false,

	--// Trails \\--
	["FireTrail"] = false,

	--// Pets \\--
	["Puffle"] = false,
-- etc
}

Players.PlayerAdded:Connect(function(Player)
	local BoolDataStore = nil
	
	local bools = Instance.new("Folder")
	bools.Name = "Bools"
	bools.Parent = Player
	
	for name, value in pairs(data) do
		local new = Instance.new("BoolValue")
		new.Name = name
		new.Value = value
		new.Parent = bools
	end
	
	local s, e = pcall(function()
		BoolDataStore = BoolTestDataStore:GetAsync("uid-" .. Player.UserId)
	end)
	
	if s then
		print("Getting Bool Data For: " .. Player.Name)
		if BoolDataStore then
			for name, value in pairs(BoolDataStore) do
				Player.Bools[name].Value = value
			end
			print("Bool Data Loaded For: " .. Player.Name)
		else
			print("Created New Bool Data For: " .. Player.Name)
		end
	else 
		warn("Error Getting Bool Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local BoolDataStore = {}
	
	for name, value in pairs(Player.Bools:GetChildren()) do
		BoolDataStore[value.Name] = value.Value
	end
	
	local success = pcall(BoolTestDataStore.SetAsync, BoolTestDataStore, "uid-" .. Player.UserId, BoolDataStore)
	
	if success then
		print("Successfully Saved Bool Data For: " .. Player.Name)
	end
end)

What I have provided you with, is an example of a way I datastore my boolean values. This way can be found, online, as there’s multiple different ways to do this.
In order to communicate this with a server, you need to have a RemoteEvent inside of a shop gui (assuming this is how you’re doing it), which communicates with this RemoteEvent which fires(LocalScript inside the Button in the UI), then a ServerScript in ServerScriptService which then communicates with the DataStore, example;

game.ReplicatedStorage.CharStoreRemote.OnServerEvent:Connect(function(player,char,price)
	player.Bools:FindFirstChild(char).Value = true
	player.stats.Bloxcoins.Value = player.stats.Bloxcoins.Value - price
end)

Above is a ServerScript which communicates with the DataStore Script, after the UI Button has been pressed, and the remote event fires.

If I was unclear, or you need deeper explanation, please contact me via here, or discord, BryanFehr#3070. Hopefully I could be of service for you, and everyone else asking the same questions.

NOTE This script provided is for educational/learning purposes only!! This is in NO WAY a “given” or “free” script, and is for YOUR LEARNING PURPOSE ONLY.

4 Likes