Hello everybody, So whenever the player clicks Equip on the cat, the cat clones in his/her character and then follows the player around. The issue is that the player can click Equip multiple times and the cat will clone infinitely. Is there any way to prevent this
Local Script
local Equip = script.Parent
local template = script.Parent.Parent.Parent.Parent.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
Equip.MouseButton1Click:Connect(function() -- when player clicks Equip, the Equip is a Ui that prompts players
Equip.Text = "Equipped"
local EquippedValue = template:WaitForChild("EquippedValue")
EquippedValue.Value = "Equipped" -- value in template to store info
template.check.ImageColor3 = Color3.fromRGB(82, 170, 55)
for _, child in pairs(template:GetChildren()) do -- checking all children of template
if child:IsA("Folder") and child.Name == "Cat" then
print("Cat is a valid child of template")
EquipPet:FireServer()
end
end)
Server Script ā this script deals with positioning and oreientation of the pet
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipPet = ReplicatedStorage:WaitForChild("EquipPet")
EquipPet.OnServerEvent:Connect(function(player)
local char = player.Character
local Cat = game.ReplicatedStorage.Pets.BasicEggPets.Cat:Clone()
local humroot = char.HumanoidRootPart
--local i = 1
--while i == 1 do
if Cat ~= nil and char ~= nil then
Cat:SetPrimaryPartCFrame(humroot.CFrame)
local modelSize = Cat.PrimaryPart.Size
local attachmentCharacter = Instance.new("Attachment")
attachmentCharacter.Visible = false
attachmentCharacter.Parent = humroot
attachmentCharacter.Position = Vector3.new(1,-5.5,0) + modelSize
local attachmentPet = Instance.new("Attachment")
attachmentPet.Visible = false
attachmentPet.Parent = Cat.PrimaryPart
local alignPosition = Instance.new("AlignPosition")
alignPosition.MaxForce = 25000
alignPosition.Attachment0 = attachmentPet
alignPosition.Attachment1 = attachmentCharacter
alignPosition.Responsiveness = 25
alignPosition.Parent = Cat
local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.MaxTorque = 25000
AlignOrientation.Attachment0 = attachmentPet
AlignOrientation.Attachment1 = attachmentCharacter
AlignOrientation.Responsiveness = 25
AlignOrientation.Parent = Cat
Cat.Parent = char
db = false
--end
end
end)