Attempt to index nill with 'name'

Iam making a simulator game with a friend but I was scripting the pets but I run into a problem but can’t fix it

I can’t buy the pet and the output says ‘attemt to index nill with ‘name’’

I tried pretty much everything and im not really a scripter so-

I hope someone can help me

the script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats'
	
	local Pets = Instance.new('Folder', player)
	Pets.Name = 'Pets'
	
	local coins = Instance.new('IntValue', leaderstats)
	coins.Name = 'Coins'
	coins.Value = 15
	
	player.CharacterAdded:Connect(function(char)
		local attachement = Instance.new('Attachment', char.HumanoidRootPart)
		attachement.Name = 'CharacterAt'
	end)
end)

game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player,ammount)
	local currency = 'Coins'
	player.leaderstats[currency].Value = player.leaderstats[currency].Value
end)

game.ReplicatedStorage.RemoteFunctions.EquipPet.OnServerInvoke = function(player, pet)
	local currency = 'Coins'
	local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)
	
	if not player.Pets: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()
			clonedPet.Parent = player.Character
			clonedPet:SetPrimaryPartCFrame(player.Character.Head.CFrame)
			
			local atPet = Instance.new('Attachment', clonedPet.PrimaryPart)
			
			local at1 = Instance.new('Attachment', player)
			local ap = Instance.new('AlignPosition')
			ap.Parent = clonedPet
			ap.RigidityEnabled = true
			ap.Attachment0 = atPet
			ap.Attachment1 = player.Character.HumanoidRootPart.CharacterAt
			
			
			return 'Bought'
		else
			return 'Not enough coins'
		end
	else
		return 'Equip'
	end
end

I’m assuming the error occurs at
if not player.Pets:FindFirstChild(MainPet.Name) then
could you post the code that invokes this and sends the “pet” parameter or tell me what it is exactly?

in here, maybe you didn’t set anything to the mainpet variable

Do you know how to fix it so it works?

wait you did, you declared the MainPet in here

maybe the thing that causing the error is this

I don’t see any pet variable

oh wait, i was wrong again lmao. sorry you did declared the pet in here

Idk im not a scripter or so so idk

oh, who wrote the script?

nothing in her

Me but its from a youtube tutorial

I think the problem is in here because you’re trying to find something that doesn’t exist

in here you used the pet it doesn’t actually store anything to the variable

I suggest you find another tutorial or just learn the basic of scripting so you can actually make your own script

anyways, good luck!

Rip I worked 5 hours on it

Do you know a good tutorial

Here, I looked up some tutorials for you.

I will try with these the youtuber qlso has a new tutorial for a simulator so

Good Luck!

nothing again

I want to learn to script but idk hoeto script

There are many tutorials on YT about scripting but I know these guys have good tutorials on things.

AlvinBlox
TheDevKing
HowToRoblox

1 Like

in that part of :FindFirstChild(), for anyone that is with this problem, i recommend using tostring(), an example, Instance:FindFirstChild(tostring(Part.Name))

go read the api reference before asking me or trying to do anything with tostring(), Strings (roblox.com)

Yessir.

Summary

Nothing here