Hey can someone help me with this

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

  1. What do you want to achieve? Keep it simple and clear!
    I created a pet shop where when I buy a pet, the pet doesn’t immediately appear in my inventory, but if I reset the character, the pet will appear. so how to solve this?

  2. What is the issue? Include screenshots / videos if possible!

Script Buy Pet


local rs = game.ReplicatedStorage
local Event = rs.Event
local PetFox = 100

local PetsBuy = rs.TokoPet

Event.Fox.OnServerEvent:Connect(function(p)
	if p.leaderstats.Cash.Value >= PetFox then
		if p.Pets:FindFirstChild("Fox") == nil then
				local val = Instance.new("StringValue")
				val.Name = "Fox"
				val.Parent = p.Pets
				INFO:FireClient(p)
			p.CharacterAdded:Connect(function()
			local val = Instance.new("StringValue")
			val.Name = "Fox"
			val.Parent = p.Pets
			end)
		else
			SAYA:FireClient(p)
		end
	else
		if p.leaderstats.Cash.Value < PetFox then
			MarketplaceService:PromptProductPurchase(p, rs.ProdukID.Fox.Value)
		end
	end
end)

LocalScript inventory

local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService") 
local tweenService = game:GetService("TweenService")
local uis = game:GetService("UserInputService")

local abbvModule = require(replicatedStorage:WaitForChild("Abbv_Module"))

local player = game.Players.LocalPlayer

local Module3D = require(replicatedStorage:WaitForChild("Module3D"))

local frame = script.Parent
local info = frame.Info
local displayPet = info.DisplayPet
local container = frame.Container
local template = container.Template
local petNameDisplayInfo = info.PetName
local equipBTN = info.Equip
local unequipAll = frame.UnequipAll
local delete = info.Delete

local selectedTemplate = nil

local function onEquip()
	if selectedTemplate ~= nil then
		if selectedTemplate:FindFirstChild("Equipped").Value == false then
			local result = replicatedStorage.EggHatchingRemotes.EquipPet:InvokeServer(selectedTemplate.Name)

			if result == "Equip" then
				selectedTemplate.Checkmark.Visible = true
				equipBTN.TextLabel.Text = "Unequip"
				selectedTemplate:FindFirstChild("Equipped").Value = true
			elseif result == "Unequip" then
				equipBTN.TextLabel.Text = "Equip"
				selectedTemplate.Checkmark.Visible = false

				selectedTemplate:FindFirstChild("Equipped").Value = false
			end
		else
			local result
			pcall(function()
				result = replicatedStorage.EggHatchingRemotes.UnequipPet:InvokeServer(selectedTemplate.Name)
			end)
			
			if result == true or selectedTemplate:FindFirstChild("Equipped").Value == true then
				equipBTN.TextLabel.Text = "Equip"
				selectedTemplate.Checkmark.Visible = false


				selectedTemplate:FindFirstChild("Equipped").Value = false
			end
		end

	end
end

--local function onEquipBest()
--	for i,v in pairs(container:GetChildren()) do
--		if v ~= nil and v:IsA("TextButton") then
--			v.Checkmark.Visible = true
--			equipBTN.TextLabel.Text = "Unequip"
--			v:FindFirstChild("Equipped").Value = true
--		end
--	end
	
--	local player_Pets = {}
--	local pets_multipliers = {}

--	for i,v in pairs(player.Pets:GetChildren()) do
--		table.insert(player_Pets, v)
--	end

--	print(player_Pets)

--	for i,petName in pairs(player_Pets) do
--		local pet
--		if replicatedStorage.Pets["Basic Egg"]:FindFirstChild(tostring(petName)) then
--			pet = replicatedStorage.Pets["Basic Egg"]:FindFirstChild(tostring(petName))
--		elseif replicatedStorage.Pets["Red Egg"]:WaitForChild(tostring(petName)) then
--			pet = replicatedStorage.Pets["Red Egg"]:FindFirstChild(tostring(petName))
--		else
--			pet = nil
--		end

--		if pet == nil then
--			print("Pet is nil")
--		else
--			table.insert(pets_multipliers, pet.Multiplier1.Value)
--		end
--	end

--	local allpets = player.Pets:GetChildren()

--	for i = 1,tonumber(#allpets) do
--		local numberOfPetsEquipped = #workspace.Player_Pets:FindFirstChild(player.Name):GetChildren()

--		if (numberOfPetsEquipped + 1) <= player.Values.MaxPetsEquipped.Value then
--			print(i)
--			local highestNumber = math.max(table.unpack(pets_multipliers))

--			local numberOfPetsEquipped = #workspace.Player_Pets:FindFirstChild(player.Name):GetChildren()

--			local clonedPet

--			print("Current Highest Un-equipped Pet Multiplier is"..highestNumber)

--			for _,v in pairs(replicatedStorage.Pets:GetDescendants()) do
--				if v:IsA("Model") then
--					if tonumber(v.Multiplier1.Value) == highestNumber then
--						print("Pet Has Been Found!")
--						clonedPet = v:Clone()
--					end
--				end
--			end

--			clonedPet.Parent = workspace.Player_Pets:FindFirstChild(player.Name)
--			if player.Values.Multiplier1.Value == "1" then
--				player.Values.Multiplier1.Value = player.Values.Multiplier1.Value + (clonedPet.Multiplier1.Value - 1)
--			else
--				player.Values.Multiplier1.Value = player.Values.Multiplier1.Value + clonedPet.Multiplier1.Value

--			end

--			local currentHighestNum = table.find(pets_multipliers, highestNumber)
--			table.remove(pets_multipliers, currentHighestNum)
--			print(pets_multipliers)


--			task.wait()
--		end

--	end
--end

local function onTemplatePress(temp)
	selectedTemplate = temp
	if selectedTemplate:FindFirstChild("Equipped").Value == true then
		equipBTN.TextLabel.Text = "Unequip"
		selectedTemplate.Checkmark.Visible = true

	else
		selectedTemplate.Checkmark.Visible = false

		equipBTN.TextLabel.Text = "Equip"
	end
	petNameDisplayInfo.Text = temp.Name
	
	
	for i, v in pairs(displayPet:GetChildren()) do 
		if v:IsA("ViewportFrame") then
			v:Destroy()
		end
	end
	
	info.Multiplier1.Text = "x"..abbvModule.abbvNum(tonumber(replicatedStorage.Pets:FindFirstChild(temp.Name, true).Multiplier1.Value)).." Coins"
	
	local petModel = Module3D:Attach3D(displayPet,replicatedStorage.Pets:FindFirstChild(temp.Name, true):Clone())
	petModel:SetDepthMultiplier(1.2)
	petModel.Camera.FieldOfView = 5
	petModel.Visible = true

	runService.RenderStepped:Connect(function()
		petModel:SetCFrame(CFrame.Angles(0,tick() * 2 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
	end)
end

local function deletePet()
	if selectedTemplate ~= nil then
		replicatedStorage.EggHatchingRemotes.DeletePet:FireServer(selectedTemplate.Name)
		
		selectedTemplate:Destroy()
		selectedTemplate = nil
		
		petNameDisplayInfo.Text = "Nothing"


		for i, v in pairs(displayPet:GetChildren()) do 
			if v:IsA("ViewportFrame") then
				v:Destroy()
			end
		end

		info.Multiplier1.Text = "x0"
		
		equipBTN.TextLabel.Text = "EQUIP"
	end
end

_G.newTemplate = function(petName)


	local exists = container:FindFirstChild(petName)
	local newTemplate

	if exists then
		newTemplate = exists
		newTemplate:FindFirstChild("AmountGG").Value += 1
	else
		newTemplate = template:Clone()
		newTemplate.Name = petName
		newTemplate.Visible = true
		newTemplate.Parent = container
	end

	local petModel = Module3D:Attach3D(newTemplate.Display,replicatedStorage.Pets:FindFirstChild(petName, true):Clone())
	petModel:SetDepthMultiplier(1.2)
	petModel.Camera.FieldOfView = 30
	petModel.Visible = true

	runService.RenderStepped:Connect(function()
		petModel:SetCFrame(CFrame.Angles(0,tick() * 0 % (math.pi * 2),0) * CFrame.Angles(math.rad(-10),0,0))
	end)

	newTemplate.MouseButton1Click:Connect(function()
		onTemplatePress(newTemplate)
	end)
end

equipBTN.MouseButton1Click:Connect(function()
	onEquip()
end)


unequipAll.MouseButton1Click:Connect(function()
	for i, v in pairs(container:GetChildren()) do 
		if v:IsA("TextButton") then
			v:FindFirstChild("Equipped").Value = false
			v.Checkmark.Visible = false
		end

	end
	replicatedStorage.EggHatchingRemotes.UnequipAll:FireServer()
end)

delete.MouseButton1Click:Connect(function()
	local deletePetConfirmation = script.Parent.DeletePetConfirmation
	deletePetConfirmation.Visible = true
	deletePetConfirmation:TweenPosition(UDim2.new(0.236, 0,0.28, 0),"Out","Sine",0.1)
	
	deletePetConfirmation.Yes.MouseButton1Click:Connect(function()
		deletePet()
		deletePetConfirmation.Visible = false
	end)
	
	deletePetConfirmation.No.MouseButton1Click:Connect(function()
		deletePetConfirmation.Visible = false
		deletePetConfirmation.Position = UDim2.new(0.232, 0,0.321, 0)
		return
	end)
end)

wait(2)

for i, v in pairs(player.Pets:GetChildren()) do 
	_G.newTemplate(v.Name)
end

Egg_System [Main_Server]
Script

local Players = game:GetService("Players")
local mps = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("12345678910")
--local tripleHatchID = 29757481
local tripleHatchID = 24274065
local autoHatchId = 32338581

local PetsFolder = workspace:WaitForChild("Player_Pets")

local function playerAdded(player)
	local folder = Instance.new('Folder')
	folder.Name = player.Name
	folder.Parent = PetsFolder
	
	for i, v in pairs(game.ServerStorage.EggHatchingData:GetChildren()) do 
		v:Clone().Parent = player
	end
	
	if mps:UserOwnsGamePassAsync(player.UserId, tripleHatchID) then
		player.Values:FindFirstChild("CanTripleHatch").Value = true
	end
	
	if mps:UserOwnsGamePassAsync(player.UserId, autoHatchId) then
		player.Values:FindFirstChild("CanAutoHatch").Value = true
	end
	
	local data = dataStore:GetAsync(player.UserId.."Pets")
	
	if data ~= nil then
		for i, v in pairs(data) do 
			local val = Instance.new("StringValue")
			val.Name = v
			val.Parent = player.Pets
		end
	end
	

end

local function PlayerRemoving(player)
	if PetsFolder:FindFirstChild(player.Name) then
		PetsFolder:FindFirstChild(player.Name):Destroy()
	end
	local pets = {}
	
	for i, v in pairs(player.Pets:GetChildren()) do 
		table.insert(pets, v.Name)
	end
	
	dataStore:SetAsync(player.UserId.."Pets", pets)
end

Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

for i, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end

local function choosePet(egg)
	local Chance = math.random(1,100)
	local Counter = 0
	for _,pet in pairs(game.ReplicatedStorage.Pets[egg.Name]:GetChildren()) do
		Counter = Counter + pet.Rarity.Value
		if Chance <= Counter then
			return pet.Name
		end
	end
end

game.ReplicatedStorage.EggHatchingRemotes.HatchServer.OnServerInvoke = function(player, egg)
	local eggModel = workspace.Eggs:FindFirstChild(egg.Name)
	if eggModel ~= nil then
		local price = eggModel.Price.Value
		local currency = eggModel.Currency.Value
		local numberPrice = tonumber(price)

		if tonumber(player.leaderstats[currency].Value) >= price then
			player.leaderstats[currency].Value = player.leaderstats[currency].Value - price
			local chosenPet = choosePet(egg)
			local val = Instance.new("StringValue")
			val.Name = chosenPet
			val.Parent = player.Pets
			
			return chosenPet
		else
			return "Cannot Hatch"
		end
	end
end

game.ReplicatedStorage.EggHatchingRemotes.AutoHatch.OnServerInvoke = function(player, egg)
	if player.Values.CanAutoHatch.Value == true then
		local eggModel = workspace.Eggs:FindFirstChild(egg.Name)
		if eggModel ~= nil then
			local price = eggModel.Price.Value
			local currency = eggModel.Currency.Value
			local numberPrice = tonumber(price)


			if tonumber(player.leaderstats[currency].Value) >= price then
				player.leaderstats[currency].Value = player.leaderstats[currency].Value - price
				local chosenPet = choosePet(egg)
				local val = Instance.new("StringValue")
				val.Name = chosenPet
				val.Parent = player.Pets

				return chosenPet
			else
				return "Cannot Hatch"
			end
		end
	else
		return "The player does not own the gamepass"
	end
end

game.ReplicatedStorage.EggHatchingRemotes.Hatch3Pets.OnServerInvoke = function(player, egg)
	if player.Values.CanTripleHatch.Value == true then
		local eggModel = workspace.Eggs:FindFirstChild(egg.Name)
		if eggModel ~= nil then
			local price = eggModel.Price.Value
			local currency = eggModel.Currency.Value
			local numberPrice = tonumber(price)

			if tonumber(player.leaderstats[currency].Value) >= numberPrice * 3 then
				player.leaderstats[currency].Value = player.leaderstats[currency].Value - price * 3
				local chosenPet = choosePet(egg)
				local chosenPet2 = choosePet(egg)
				local chosenPet3 = choosePet(egg)
				
				local val = Instance.new("StringValue")
				val.Name = chosenPet
				val.Parent = player.Pets
				
				local val2 = Instance.new("StringValue")
				val2.Name = chosenPet2
				val2.Parent = player.Pets
				
				local val3 = Instance.new("StringValue")
				val3.Name = chosenPet3
				val3.Parent = player.Pets

				return chosenPet, chosenPet2, chosenPet3
			else
				return false
			end
		end
	else
		return "The player does not own the gamepass"
	end

end

game.ReplicatedStorage.EggHatchingRemotes.EquipPet.OnServerInvoke = function(player, petName)
	local numberOfPetsEquipped = #workspace.Player_Pets:FindFirstChild(player.Name):GetChildren()
	
	if (numberOfPetsEquipped + 1) <= player.Values.MaxPetsEquipped.Value then
		local clonedPet = game.ReplicatedStorage.Pets:FindFirstChild(petName, true):Clone()
		
		clonedPet.Parent = workspace.Player_Pets:FindFirstChild(player.Name)
		if player.Values.Multiplier1.Value == "1" then
			player.Values.Multiplier1.Value = player.Values.Multiplier1.Value + (clonedPet.Multiplier1.Value - 1)
		else
			player.Values.Multiplier1.Value = player.Values.Multiplier1.Value + clonedPet.Multiplier1.Value

		end
		return "Equip"
	elseif (numberOfPetsEquipped + 1) > player.Values.MaxPetsEquipped.Value then
		return "Cannot Equip"
	end
end

game.ReplicatedStorage.EggHatchingRemotes.UnequipPet.OnServerInvoke = function(player, petName)
	local pet = workspace.Player_Pets:FindFirstChild(player.Name):FindFirstChild(petName)
	
	if player.Values.Multiplier1.Value == pet.Multiplier1.Value then
		player.Values.Multiplier1.Value = "1"
	else
		player.Values.Multiplier1.Value -= pet.Multiplier1.Value
	end
	
	pet:Destroy()
	return true
end

game.ReplicatedStorage.EggHatchingRemotes.UnequipAll.OnServerEvent:Connect(function(player)
	local playerPets = workspace.Player_Pets:FindFirstChild(player.Name)

	playerPets:ClearAllChildren()
	player.Values.Multiplier1.Value = "1"
end)

mps.PromptGamePassPurchaseFinished:Connect(function(player, gamepassID, purchased) 
	if gamepassID == tripleHatchID then
		if purchased == true then
			player.Values:FindFirstChild("CanTripleHatch").Value = true
		end
	end
end)

game.ReplicatedStorage.EggHatchingRemotes.Test.OnServerEvent:Connect(function(player)
	player.leaderstats.Coins.Value += (1 * tonumber(player.Values.Multiplier1.Value) )
end)

game.ReplicatedStorage.EggHatchingRemotes.DeletePet.OnServerEvent:Connect(function(player, petName)
	if player.Pets:FindFirstChild(petName) then
		player.Pets:FindFirstChild(petName):Destroy()
		
		local pet = workspace.Player_Pets:FindFirstChild(player.Name):FindFirstChild(petName)
		
		if pet ~= nil then
			if player.Values.Multiplier1.Value == pet.Multiplier1.Value then
				player.Values.Multiplier1.Value = "1"
			else
				player.Values.Multiplier1.Value -= pet.Multiplier1.Value
			end

			pet:Destroy()
		end
	end
end)

mps.PromptGamePassPurchaseFinished:Connect(function(player, gamepassID, purchased) 
	if gamepassID == autoHatchId then
		if purchased == true then
			player.Values:FindFirstChild("CanAutoHatch").Value = true
		end
	end
end)

game:BindToClose(function()
	wait(1)
end)

By the looks of it you only generate the inventory once, when the UI is created. I’m assuming you have ResetOnSpawn enabled for your ScreenGui meaning the inventory is only generated when a player respawns.
Use an Event (Bindable or Remote) to update it when a pet is obtained.
Also, what’s the wait(2) for?

1 Like

I would get the Pets Folder and do the ChildAdded Event shout be something like this. You not checking for it something has been added to the folder, you should also check if a item has been remove from the folder with ChildRemoved.

player.Pets.ChildAdded:Connect(function(newPet)
    if newPet and newPet:IsA("StringValue") then
        _G.newTemplate(newPet.Name)
    end
end)
1 Like