I am trying to make the pet spawn next to you but there is an error when you press the buy button.

This is the scripts it is referring to: Local Script 2:

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

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

3 comments

0

Can you please elaborate more. And please properly format your code.

0

To properly format your code so it is easier to read, just use the lua button above where you type and then type between the ~~~

0

I fixed it now please help :)