StringValues share names sometimes in a folder, what to use to destroy() only one of them instead of all?

  1. What do you want to achieve? Keep it simple and clear!

I want to be able to sell a pet from my inventory, sometimes it’s possible to get the same pet. Which produces the exact same name and instead of stacking, creates a separate string value with the name of the pet.

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

I’ve been able to get it to remove the string with my script below. However if any other strings share the same name it destroys that as well.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Due to lack of knowledge on any other function similar to Destroy() or if its possible to utilize it in a way that only destroys one of possibly many of the same items with the same name.

Script is here and I’ve indicated where the issue is though in this case it may not produce any additional information as I’m sure that someone can simply answer the question.

local replicatedStorage = game:GetService("ReplicatedStorage")
local physicalPet = script.Parent.Parent
local PetInventory = game.Players.LocalPlayer.PetInventory



script.Parent.Activated:Connect(function(player)
	if script.parent.Text == "Sell" then
		script.Parent.Text = "Sure?"
		coroutine.wrap(function()
			wait(3)
			script.parent.Text = "Sell"
		end)()	
	elseif script.Parent.Text == "Sure?" then
		for i, pet in pairs(PetInventory:GetChildren()) do
			if pet:IsA("StringValue") then
				pet:Destroy() -- here lies the issue. 
			end
		end
		physicalPet:Destroy()
		game.Players.LocalPlayer.leaderstats.Candy.Value = game.Players.LocalPlayer.leaderstats.Candy.Value + 5
		
		
		
	
	end
end)

I hope that its possible I may be able to save my pet system lol.

If you only want to destroy one of them, instead of iterating you could just use PetInventory:FindFirstChildOfClass("StringValue")
That will select just one of the folder’s stringvalues.

3 Likes

OMG TY! This is amazing you may have just saved my game lol

I’d hate to dig this back up, But I’ve found an issue with server side deletion of the string value. Attempting to implement it into a fire server function I’m unsure how to get it to remove from the server with the precision that I’m having with the client side. the string values are in a specific order in the pet inventory. So it simply deletes the first one instead of the pet its supposed to be tied to.

Script in here so as not to take up the whole screen
===================LocalScript================================
local replicatedStorage = game:GetService("ReplicatedStorage")
local physicalPet = script.Parent.Parent
local PetInventory = game.Players.LocalPlayer.PetInventory



script.Parent.Activated:Connect(function(player)
	if script.parent.Text == "Sell" then
		script.Parent.Text = "Sure?"
		coroutine.wrap(function()
			wait(3)
			script.Parent.Text = "Sell"
		end)()	
	elseif script.Parent.Text == "Sure?" then
		if game.Players.LocalPlayer.EquippedPet.Value == PetInventory:FindFirstChildOfClass("StringValue").Name then
			script.Parent.Text = "Unequip"
			wait(1)
			script.Parent.Text = "Sell"
		else 
			physicalPet:Destroy()
			PetInventory:FindFirstChildOfClass("StringValue"):Destroy()
			game.ReplicatedStorage.Functions.SellPet:FireServer(player)
		end
	end
end)
===================Script======Just the block that is needed here=============
game.ReplicatedStorage.Functions.SellPet.OnServerEvent:Connect(function(plr)
	local PetInventory = plr.PetInventory
	local PetName = plr.PlayerGui.Inventory.Pets.ScrollingFrame
	
	plr.leaderstats.Candy.Value = plr.leaderstats.Candy.Value + 5
	PetInventory:FindFirstChildOfClass("StringValue"):Destroy()
	
end)

Maybe you should use some kind of more unique identifier for each pet, rather than the type of pet it is, since your server code can’t seem to tell the difference. If the player is choosing a specific instance, then you need to send more information through the event than just the name, so the server can find the right one by matching it’s properties with the properties received from the event.

1 Like

I was thinking that too i appreciate it man