I want to be able to get passed this script error that has been blocking my progress
I haven’t looked for many solutions
Pet folder
Local Script Location
Main script and pet location
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)
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
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?