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)
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
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.
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
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