Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to fix? ServerScriptService.Main:30: attempt to index nil with 'Name' - Client - LocalScript:2

This is the second time i have posted this. Someone please help. So the problem is, I have a pet store, when you walk over it a gui comes up very well there is a scrolling place there is a close button. But when you try to press the buy button and you have enough money It comes up with this error 'ServerScriptService.Main:30: attempt to index nil with 'Name' - Client - LocalScript:2' and does nothing.

LocalScript:2

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

ServerScirptService Main:

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 = 0
	
	player.CharacterAdded:Connect(function(char)
		local attachment = Instance.new('Attachment', char.HumanoidRootPart)
		attachment.Name = 'CharacterAt'
		attachment.Position = Vector3.new(3,3,0)
	end)
end)


game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player, ammount)
	local currency = 'Coins'
	local ammount = 1000
	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.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 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

2 comments

0

The error is that MainPart is nil. Make sure captilisation and other stuff is correct. Let me know if it resolved your issue.

0

Could you please let me know how to do this and in what script? I'm trying but nothing is working. Thanks.

1 answer

This error means that Mainpet has no value (nil). You didn't send in an argument for the pet during :InvokeServer(). Because of this, Mainpet also has a value of nil. You need to send in a pet argument when doing InvokeServer

This discussion is preserved as it was. New answers, comments, and votes are closed.