Issues with buying pet script

NOTE [SORRY IF THIS IS ALOT TO TAKE IN] :sweat_smile: :grimacing:

Hey, guys so I’m having an issue with the buy pet script. I have tried to change the PetShop frames, and renaming them, as well as changing some of the functions in the script but nothing is seeming to work. I have an output message and it looks like this…

When I headed over to ServerStorage…
Screen Shot 2021-08-31 at 9.51.34 PM

I obviously have a Folder named “Pets” with the pets in them, I’m not yet having issues with the pet going behind me, but equipping it first. I also have a RemotesFunctions folder…
Screen Shot 2021-08-31 at 9.52.32 PM

I have the script I typed below

Equip Pet Script

script.Parent.MouseButton1Click:Connect(function()

local result = game.ReplicatedStorage.RemoteFunctions.EquipPet:InvokeServer(script.Parent.Parent.Name)

end)

Main Script – Including leaderboards, and the Rigidity to follow the player

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 = 5000
	
	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, ammount)
	local currency = 'Coins'
	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.Parent.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

if you guys have any solutions or anyway I can try and fix this please tell me!

1 Like

try move your folder pets to replicatedstorage

1 Like

Where did the error occur? From the local script?

and use a print for it like when OnServerInvoke print(pet)

local MainPet = game.ServerStorage.Pets:FindFirstChild(pet)
print(MainPet)

Make sure that is correct and on your localscript print script.Parent.Parent.Name

1 Like

can you screen shot your frame locate and localscript locate

1 Like

The error occurred on line 2 of the local script

Wait I am confused. The image that is showing the error is saying that the error is occuring on line 2 in a LocalScript…? And I am pretty sure I have found the actual code line that is causing the error

The problem here is that the MainPet’s parent is the folder named Pets, and you are trying to say that there is a Value property. But there is no Value property for Folder instances. So the script will assume that Value is a child of the folder.

1 Like

well, the value part on the script was stating that the currency is goign to be taking from the pet you buy, so like the money will be subtracteed.

So how exactly could i make it so the Value in in the folder?

Looking at your script again, I see you already have a value inside each pet. So I guess there is no need for doing anything extra.

Here is your full (updated) code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local RemoteFunctions = ReplicatedStorage:WaitForChild("RemoteFunctions")
local RemoteEvents = ReplicatedStorage:WaitForChild("Remotes")

local EquipPetFunction = RemoteFunctions.EquipPet
local AddEvent = RemoteEvents.Add
local PetsFol = ServerStorage:WaitForChild("Pets")

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 = 5000
	
	player.CharacterAdded:Connect(function(character)
		local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local attachment = Instance.new('Attachment', HumanoidRootPart)

		attachment.Name = 'CharacterAt'
	end)	
end)

AddEvent.OnServerEvent:Connect(function(player, amount)
	local currency = 'Coins'
	player.leaderstats:FindFirstChild(currency).Value += amount
end)

EquipPetFunction.OnServerInvoke = function(player, pet)
	local currency = 'Coins'
	local MainPet = PetsFol:FindFirstChild(pet)
	
	if not player.Pets:FindFirstChild(MainPet.Name) then
		if player.leaderstats[currency].Value >= MainPet.Price.Value then
			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
1 Like

Thanks! The buying part worked, but when I equip the pet it launches my body on the floor like an ice rank, ill get back to you when I try and get this bug fixed.

1 Like

Alright I got it fixed! attachment.Position = Vector3.new(3,3,0)

1 Like

If he helped you fix your issue you should mark it as a solution so other people will know where to look if they have a similar issue as you, as well as for the guy to benefit by it in his own ways.

Good luck with your project.

1 Like

Thanks for letting me know! I’ll post the game when it releases so you can play if you’d like!

1 Like