Need help with making a system that keeps track of how many an item exists

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Making a system that keeps track of how many an item exists

  2. What is the issue? I tried making a system that keeps track of how many an item exists but it didn’t work

  3. What solutions have you tried so far? I looked for solutions on the forum, but they weren’t really helpful.

Edit: I still haven’t found out how to make the exist system (read the posts below for more info)

1 Like

What do you mean by amount of items? what items are they.
If you mean, for example, amount of items in the workspace you could do:

local Items = 0
local ItemHolder = game.Workspace
while task.wait(1) do
   Items = #ItemHolder:GetDescendants()
end

No. What I meant by that was making a system that keeps track of how much an item exists (like in pet simulator x)

I don’t play pet simulator X, what’s the “0 exists” thing counting

It counts how much of a pet exists

It says 0 when there is clearly 1 of them?

Yeah, it was just broken at that time.

Use the previous code but change it up a bit to count only when a desired pet exists (the instances MUST be exact with this code):

local Items = 0
local ItemHolder = workspace
local Reference = workspace.Pet
while task.wait(1) do
   Items = 0
   for _, Item in pairs(ItemHolder:GetChildren()) do
      if Item == Reference then Items += 1 end
   end
end

Edit: If you want to go off names, use this code instead (doesnt have to be exact, but must have the same name):

local Items = 0
local ItemHolder = workspace
local Reference = workspace.Pet
while task.wait(1) do
   Items = 0
   for _, Item in pairs(ItemHolder:GetChildren()) do
      if Item.Name == Reference.Name then Items += 1 end
   end
end

Main Script: – Basically the datastore (Server Script)

local dss = game:GetService("DataStoreService")
local save = dss:GetDataStore("ExistsValue")


for i, ExistsValue in pairs(game.ReplicatedStorage.Pets_Folder:GetChildren()) do
	local Exists = ExistsValue:FindFirstChild("Exists")
	
	game:BindToClose(function()
		save:SetAsync("ExistsValue", Exists.Value)
	end)
	while wait(math.random(5, 10)) do
		save:SetAsync("ExistsValue", Exists.Value)
	end
	
	local saveddata = save:GetAsync("ExistsValue", Exists.Value)
	Exists.Value = saveddata
	print(Exists.Value.." exists of "..ExistsValue)
end

The script that gets the number of how much a pet exists: - Local Script

local Index = script.Parent:WaitForChild("Index")
local InfoFrame = Index:WaitForChild("Info")
local PetImage = InfoFrame:WaitForChild("Image"):WaitForChild("PetImage")
local PetName = InfoFrame:WaitForChild("PetName")
local Description = InfoFrame:WaitForChild("Description")
local scrollingFrame = Index:WaitForChild("ScrollingFrame")
local template = scrollingFrame:WaitForChild("Template")
local Pets = RS:WaitForChild("Pets_Folder")
local Click = script.Parent:WaitForChild("Click")
local Difficulty = InfoFrame:WaitForChild("Difficulty")
local plr = game:GetService("Players").LocalPlayer
local Remote2 = game.ReplicatedStorage.GetPetExistsValue

local selectedTemplate = nil

local function selectTemplate(temp)
	InfoFrame.Visible = true
	selectedTemplate = template
	PetImage.Image = temp.PetImage.Image
	PetName.Text = "Pet Name: "..temp.Name
	Description.Text = RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name).Description.Value
	Difficulty.Difficulty.Text = "Difficulty: "..RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name).Difficulty.Value
	InfoFrame.Exists.Text = Remote2:InvokeServer(temp.Name).." Exists" --Here's where I get the number of how much of a pet exists
	if RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name).Difficulty.Value == "Exclusive" then
		Difficulty.ExclusiveGradient.Enabled = true
		Difficulty.BackgroundTransparency = 0
	end
	if RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name).Difficulty.Value ~= "Exclusive" then
		Difficulty.ExclusiveGradient.Enabled = false
		Difficulty.BackgroundTransparency = 1
	end
	
	
	local NoteTemplate = RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name)
	
	if NoteTemplate:FindFirstChild("Note") then
		InfoFrame.Note.Visible = true
		InfoFrame.Note.Text = RS:FindFirstChild("Pets_Folder"):WaitForChild(temp.Name).Note.Value
	else
		InfoFrame.Note.Visible = false
		InfoFrame.Note.Text = "NaN"
	end
end

for i, v in pairs(Pets:GetChildren()) do
	local newTemplate = template:Clone()
	newTemplate.Name = v.Name
	newTemplate.Parent = scrollingFrame
	newTemplate.Visible = true
	
	newTemplate.PetImage.Image = v:WaitForChild("Image").Texture
	
	local Handler = RS:FindFirstChild("Pets_Folder"):FindFirstChild(newTemplate.Name)

	if Handler.Difficulty.Value == "Easy" then
		newTemplate.LayoutOrder = -7
	else
		if Handler.Difficulty.Value == "Medium" then
			newTemplate.LayoutOrder = -6
		else
			if Handler.Difficulty.Value == "Hard" then
				newTemplate.LayoutOrder = -5
			else
				if Handler.Difficulty.Value == "Insane" then
					newTemplate.LayoutOrder = -4
				else
					if Handler.Difficulty.Value == "Extreme" then
						newTemplate.LayoutOrder = -3
					else
						if Handler.Difficulty.Value == "Exclusive" then
							newTemplate.LayoutOrder = -2
						else
							if Handler.Difficulty.Value == "Event" then
								newTemplate.LayoutOrder = -1
						end
						end
					end
				end
			end
		end
	end
	
	
	newTemplate.MouseButton1Click:Connect(function()
		selectTemplate(newTemplate)
		Click:Play()
		scrollingFrame.Visible = false
	end)
end

The script used for the remote to get the number of how much a pet exists: - Server Script

local save = dss:GetDataStore("ExistsValue")
local rs2 = game:WaitForChild("ReplicatedStorage")

rs2:WaitForChild("GetPetExistsValue").OnServerInvoke = function(player, temp)
	if rs2:FindFirstChild("Pets_Folder"):WaitForChild(temp) then
		local ExistsValue = rs2["Pets_Folder"]:FindFirstChild(temp)
		local num = save:GetAsync("ExistsValue", ExistsValue)
		return num
	end
end

(this is what I tried doing, but it didn’t really work)

In here to prevent replicas from showing, you can use this (using this code means add a TextLabel of some sort named Duplicates):

local alreadyHave = {}
for i, v in pairs(Pets:GetChildren()) do
	local duplicates = 0
	if not table.find(alreadyHave, v) then
		local newTemplate = template:Clone()
		newTemplate.Name = v.Name
		newTemplate.Parent = scrollingFrame
		newTemplate.Visible = true
		table.insert(alreadyHave, v)
		for _, pet in pairs(Pets:GetChildren()) do
			if pet == v then duplicates += 1 end
		end
	else continue end
	--code inbetween
	
	newTemplate.Duplicates.Text = duplicates
end

Edit: It’s fixed, hopefully.

1 Like

You should read this post if you want a value to be the same on every single server of the game.



I assume this is what you need

1 Like

Alright, thank you I will make sure to add that to my code.

Okay. I will also try that

Thank you!

image

Read my above post as I fix the issues with it. Sorry, my coding is mixed up today.

Alright. It’s okay lol, I understand

1 Like

Ok, it should be fixed:

local alreadyHave = {}
for i, v in pairs(Pets:GetChildren()) do
	local duplicates = 0
	if not table.find(alreadyHave, v) then
		local newTemplate = template:Clone()
		newTemplate.Name = v.Name
		newTemplate.Parent = scrollingFrame
		newTemplate.Visible = true
		table.insert(alreadyHave, v)
		for _, pet in pairs(Pets:GetChildren()) do
			if pet == v then duplicates += 1 end
		end
	else continue end
	--code inbetween
	
	newTemplate.Duplicates.Text = duplicates
end

Edit: I proofread it and it should work when inserted. I hope it helps!

image
Error: Unknown Global ‘newTemplate’

Sorry, again. I forgot about that, here’s your fix:

local alreadyHave = {}
for i, v in pairs(Pets:GetChildren()) do
	local duplicates = 0
	local newTemplate
	if not table.find(alreadyHave, v) then
		newTemplate = template:Clone()
		newTemplate.Name = v.Name
		newTemplate.Parent = scrollingFrame
		newTemplate.Visible = true
		table.insert(alreadyHave, v)
		for _, pet in pairs(Pets:GetChildren()) do
			if pet == v then duplicates += 1 end
		end
	else continue end
	--code inbetween
	
	newTemplate.Duplicates.Text = duplicates
end

This is the kinda trial-and-error stuff my brain does when coding. (lol)

1 Like