Index Nil `name`

I want to be able to get passed this script error that has been blocking my progress

image

I haven’t looked for many solutions

Pet folder
image
Local Script Location
image
Main script and pet location
image

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'

	local Pet = Instance.new('Folder', player)
	Pet.Name = 'Pet'

	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = 'Coins'
	coins.Value = 0

	player.CharacterAdded:connect(function(char)
		local attachment = Instance.new("Attachment", char.HumanoidRootPart)
		attachment.Name = "CharacterAt"

	end)
end)





game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player)
	local currency = 'Coins'
	local ammount = 1

	player.leaderstats[currency].Value = player.leaderstats[currency].Value +  ammount


end)  

game.ReplicatedStorage.RemoteFunctions.EquipPet.OnServerInvoke = function(player, pet)


	local currency = "Coins"
	local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)

	if not player.Pet:FindFirstChild(MainPet.Name) then
		if player.leaderstats[currency].Value >= MainPet.Price.Value then
			player.leaderstats[currency].Value = player.leaderstats[currency].Value - MainPet.Price.Value
			local clonedPet = MainPet:Clone()
			local atPet = Instance.new("Attachment", clonedPet.PrimaryPart)

			local ap = Instance.new	("AlignPosition")
			ap.Parent = pet 
			ap.RigdigityEnabled = true 
			clonedPet.Parent = player.Character
			clonedPet:SetPrimaryPartCFrame(player.Character.Head.Cframe)
			ap.Attachment0 = atPet
			ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt
	
	




			return "Bought"
		else
			return "Not enough coins"
		end
	else
		return "Equip"
	end
end

This is the local script it connects to

script.Parent.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.RemoteFunctions.EquipPet:InvokeServer(script.Parent.Parent.Name)
end)

which line is line 38?
this would help a lot

1 Like

Please don’t create a duplicate topic of this, refer to your original here:

1 Like

if the line below is line 38 then that means MainPet is nil because you didn’t put a string inside the FindFirstChild for the local MainPet line

if not player.Pet:FindFirstChild(MainPet.Name) then

I didn’t even notice that lmao

local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)

if not player.Pet:FindFirstChild(MainPet.Name) then

This part of the script is relying on a client request for the ‘pet’ string. If a server invoke comes through with a ‘pet’ that doesn’t exist in game.ServerStorage.Pets then ‘MainPet’ will become nil. Then in the next line you’ll end up trying to get the .Name of something that is nil.

To solve this you need to verify that ‘MainPet’ exists.

game.ReplicatedStorage.RemoteFunctions.EquipPet.OnServerInvoke = function(player, pet)

	local currency = "Coins"
	local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)

	if MainPet ~= nil then
		if not player.Pet:FindFirstChild(MainPet.Name) then
			if player.leaderstats[currency].Value >= MainPet.Price.Value then
				player.leaderstats[currency].Value = player.leaderstats[currency].Value - MainPet.Price.Value
				local clonedPet = MainPet:Clone()
				local atPet = Instance.new("Attachment", clonedPet.PrimaryPart)

				local ap = Instance.new	("AlignPosition")
				ap.Parent = pet 
				ap.RigdigityEnabled = true 
				clonedPet.Parent = player.Character
				clonedPet:SetPrimaryPartCFrame(player.Character.Head.Cframe)
				ap.Attachment0 = atPet
				ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt
	
				return "Bought"
			else
				return "Not enough coins"
			end
		else
			return "Equip"
		end
	else
		return "Error"
	end
end
1 Like

Then answer it then so dont post about it if u clearly know that i need it answered

What line is the error on? This problem is fairly simple. What ever you’re trying to get the property “Name” of doesn’t exist, your code is basically trying to run nil["Name"] or nil.Name.

The problem is line 38 sorry if it wasn’t clear enough

There aren’t line numbers on your code, what line corresponds to line 38?

if not player.Pet:FindFirstChild(MainPet.Name) then
1 Like

Ok so I did this the script works but now my pet isn’t showing up so i think you conquered the error but Idk if this is making it so the pet doesn’t show up

Error description:

  • An Instance with name pet doesn’t exist in ServerStorage.Pets.
    Possible Fixes:
  • Add a check to see if an Instance with name pet exists in ServerStorage.Pets. Note that while a check removes an error, needing one implies that there is a mistake elsewhere in your code. Your local scripts probably shouldn’t be asking for pets that don’t exist.
  • Look into your code that fires the RemoteFunction. Try adding this code to see if the name sent is correct:
script.Parent.MouseButton1Click:Connect(function()
	print("Requested pet name: "..script.Parent.Parent.Name)
	local result = game.ReplicatedStorage.RemoteFunctions.EquipPet:InvokeServer(script.Parent.Parent.Name)
end)

game.ReplicatedStorage.RemoteFunctions.EquipPet.OnServerInvoke = function(player, pet)
	
	local currency = "Coins"
	local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)

	if not player.Pet:FindFirstChild(MainPet.Name) then
		if player.leaderstats[currency].Value >= MainPet.Price.Value then
			player.leaderstats[currency].Value = player.leaderstats[currency].Value - MainPet.Price.Value
			local clonedPet = MainPet:Clone()
			local atPet = Instance.new("Attachment", clonedPet.PrimaryPart)

			local ap = Instance.new	("AlignPosition")
			ap.Parent = pet 
			ap.RigdigityEnabled = true 
			clonedPet.Parent = player.Character
			clonedPet:SetPrimaryPartCFrame(player.Character.Head.Cframe)
			ap.Attachment0 = atPet
			ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt
	
	




			return "Bought"
		else
			return "Not enough coins"
		end
	else
		return "Equip"
	end
end