Inventory script not working

Hello, I am trying to make a script that equips a certain pet. How this script works is when a player has a pet in the inventory(which is the cat in this case) and they click the pet a UI will pop up asking the player to Equip the pet and when the player clicks Equip the pet should be cloned and will be put in the
character. The issue is that the script does not clone the pet into the char.
Here is the script
LOCAL SCRIPT

local Equip = script.Parent
local template = script.Parent.Parent.Parent.Parent.Parent




Equip.MouseButton1Click:Connect(function()
	
	local EquippedValue = template:WaitForChild("EquippedValue")
	
	for _, child in pairs(template:GetChildren()) do 
		
		if child:IsA("Folder")	then 
			if child.Name == "Cat" then 
				
				local ReplicatedStorage = game:GetService("ReplicatedStorage")
				local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
				
				EquipPet:FireServer()
		
			end
		end
end
	
	
	
end)

SERVER SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")

EquipPet.OnServerEvent:Connect(function(player)
	local Cat = game.ReplicatedStorage.Pets.BasicEggPets.Cat:Clone()
	local char = player.Character or player.CharacterAdded
	Cat.Parent = char
end)

Screenshot 2022-04-06 201457

Try these for your server and local script:

(LOCAL SCRIPT)

--LOCAL SCRIPT
local RS = game:GetService("ReplicatedStorage")

local Equip = script.Parent
local template = Equip.Parent.Parent.Parent.Parent
local EquipPet = RS:WaitForChild("EquipPet")

Equip.MouseButton1Click:Connect(function()
	local EquippedValue = template:WaitForChild("EquippedValue")

	for _, child in ipairs(template:GetChildren()) do 
		warn("for loop works")
		if child:IsA("Folder")	then 
			if child.Name == "Cat" then 
				warn("FOUND A CAT")
				EquipPet:FireServer()
			else
				warn("CHILD NAME IS NOT A CAT: " .. tostring(child.Name))
			end
		else
			warn("CHILD IS NOT A FOLDER")
		end
	end
end)

(SERVER SCRIPT)

--SERVER SCRIPT
local RS = game:GetService("ReplicatedStorage")

local EquipPet = RS:WaitForChild("EquipPet")

EquipPet.OnServerEvent:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local Cat = RS.Pets.BasicEggPets.Cat:Clone()
	
	if char then
		Cat.Parent = char
		print(tostring(Cat.Parent.Name))
	else
		warn("CHAR DOES NOT EXIST")
	end
end)
1 Like

This line doesn’t make sense. I think what you intended to write was

local char = player.Character or player.CharacterAdded:Wait()

But the way you’re handling it isn’t very good. You should write down on the server that the cat is equipped. Then, if the character is loaded, equip the cat. Then whenever the character spawns, check if the cat is equipped and equip it again if so.

2 Likes

I tried the method it works fine for most of the script but I got the warn message saying child is not a folder even though it is right here.
Screenshot 2022-04-06 203343
And I am giving time for the Cat to parent its self to the template Do you know what happened

I figured it out. Since I put the server script in ReplicatedStorage it was not working then I moved it to workspace and it worked now. I do not know why it did not work in Replicated Storage, but at least it works now. Thanks for the help @Downrest and @hkep. :slight_smile:

1 Like