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

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

Thank you. I’ll try that. /////

(This post hasn’t been solved yet)

Still no luck huh? Been looking around for a solution to this as well, and the only thing I can think of that could handle the traffic is an offsite database

Yeah. I was thinking of using datastores before but that wouldn’t work as well for sure.

create your own api, update your values this way, dont update your database everytime a new item is found/unboxed since that will hit database rate limits quickly, instead, cache the amoutn of new found items, and deploy the new unboxed items changes every 10-20 seconds to your database

1 Like

if youre using python, you can use asynchronous api libraries that will allow you to run tasks for caching and updating these values

an API library (in my opinion the BEST api library), FastAPI allows for asynchronous api call handling

1 Like

Hate to say it, but you’re a little late! I did end up pretty much doing exactly that tho lol.

PostgreSQL database offsite hooked up to NodeJS & Express. Game pings every 5 min or so (or when it shuts down) about what items have been spawned (or deleted) and updates accordingly. Pretty cool stuff! Lots of “trade warriors” in my game use this feature to the nth degree when figuring out a value for an item.

For anyone who finds themselves looking to create a system like this, @baukeblox12’s way and/or my way is probably your best bet. Good luck to all!

2 Likes

Haha, cool to see that you already implemented it. :smiley:

1 Like