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)

On the main pet variable it is set to look in serverstorage

You are doing .Pet which is equaled to

Which is inside the player, but in the code your looking in serverstorage. That might be a issue then again I don’t know much compared to others

1 Like

Attempt to index nil with Name means that Pet (I’m assuming you are talking about Pet) is nil. It has no value. Now, I don’t have a sure idea why. But it can be due to this. So try this.

I have heard that you are not supposed to put the Parent as the second argument of Instance.new(). So, instead do it manually like this:

local Pet = Instance.new(‘Folder’)
Pet.Parent = player
Pet.Name = ‘Pet’

I am not sure if this will work. But I’m pretty sure that doing it like this is a good practice

1 Like

That’s inside the PlayerAdded event though, so it’d result as a Unknown global variable error

Can you add printing when both of the RemoteFunctions event/functions are called?

1 Like

What do you want me replace with it in the script?

I think he means here, instead of setting the parent in the instance to set it manually

2 Likes

Yes this is what I meant. I don’t know if it will solve your problem though

1 Like

Yeah it just repeated the error message sadly

Right here, what is the variable pet set to?

“pet” is the folder inside the player
image

I think when you use FindFirstChild(pet) to assign the object to MainPet, no such object with that name is being found in ServerStorage.Pets. So it return nil. Are you sure you put the names correctly?

Well then back to this,

If pet is set to the folder inside the player, and mainpet is set to a folder in serverstorage

it is essentially looking at the player for a server storage item?

The actual pet models are inside of a serverstorage folder called “Pets”

So should I change the folder name on players to PlayerPets?

You could, it wouldn’t effect much but easier for later maybe instead of looking for MainPet.Name set a different variable to the name of it?

Alright I’ll try and i’ll inform you if it works

Bad news it didn’t work sadly enough

Try this for your OnServerInvoke function?

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

	if player.Pet:FindFirstChild(MainPet.Name) ~= nil 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

@Dieseires You didn’t even reply back to me when debugging the error :confused:

Oh ok sorry btw… forgot to tell you back