Please help me, I’m confused about where else to look for help.
I am making a Pet Shop, but I have a problem In equip pet, where if I equip a pet. the pet will clone so many if I click equip
I have searched for this problem. on Youtube and also my discord friends but they all don’t know how.
[Video Pet]
and i don’t know if this script is wrong
Code script [1]
local function equip(player,SPets)
local char = player.Character or player.CharacterAdded:Wait()
local head = char.HumanoidRootPart
local Pet = game.ReplicatedStorage.SPet[SPets].ModelPet.Inner:Clone()
Pet.Name = "Pet"
Pet.Parent = player.Character
player.PetEquipped.Value = SPets
local bodyPos = Instance.new("BodyPosition", Pet)
bodyPos.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local bodygyro = Instance.new("BodyGyro", Pet)
bodygyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
while wait() do
bodyPos.Position = head.Position + Vector3.new(2,2,5)
bodygyro.CFrame = head.CFrame
end
end
script.Parent.BuyEvent.OnServerEvent:Connect(function(player,SPets)
local ShPets = game.ReplicatedStorage.SPet[SPets]
local Price = ShPets.Price
local humrootpart = player.Character. HumanoidRootPart
if Price.Value >= Price.Value and not ShPets.Price:FindFirstChild("ID") then
Price.Value = Price.Value - Price.Value
player.LePet[SPets].Value = true
elseif player.LePet:FindFirstChild(SPets) and player.LePet[SPets].Value == true then
equip(player,SPets)
if player.Character:FindFirstChild("MeshPart") then
player.Character.Pet:Destroy()
end
end
end)
Code script [2]
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
wait(1)
local PetS = player.PetEquipped
local humrootpart = player.Character. HumanoidRootPart
if PetS and PetS.Value ~= "" and player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.LePet[PetS.Value].Value == true then
local Pet = game.ReplicatedStorage.Trails[PetS.Value].ModelPet.Inner:Clone()
Pet.Name = "Pet"
Pet.Parent = player.Character
local bodyPos = Instance.new("BodyPosition", Pet)
bodyPos.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local bodygyro = Instance.new("BodyGyro", Pet)
bodygyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
while wait() do
bodyPos.Position = humrootpart.Position + Vector3.new(2,2,5)
bodygyro.CFrame = humrootpart.CFrame
end
end
if PetS and PetS.Value ~= "" and player.LePet[PetS.Value].Value == false then
player.PetEquipped.Value = ""
end
end)
end)
Code script [3]
local Save = game:GetService("DataStoreService"):GetDataStore("SalvamentoDeLePet_")
local SaveEquip = game:GetService("DataStoreService"):GetDataStore("SalvamentoPetEquipada_")
game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder",player)
Folder.Name = "LePet"
local userdata
pcall (function()
userdata = Save:GetAsync(player.userId)
end)
if userdata then
for key, value in pairs(userdata) do
local Pets = Instance.new("BoolValue",Folder)
Pets.Name = key
Pets.Value = value
end
for i,v in pairs(game.ReplicatedStorage.SPet:GetChildren()) do
if v:IsA("Folder") and not player.LePet:FindFirstChild(v.Name) then
local Pets = Instance.new("BoolValue",Folder)
Pets.Name = v.Name
Pets.Value = false
end
end
else
for i,v in pairs(game.ReplicatedStorage.SPet:GetChildren()) do
if v:IsA("Folder") then
local Pets = Instance.new("BoolValue",Folder)
Pets.Name = v.Name
Pets.Value = false
end
end
end
local Equip = Instance.new("StringValue",player)
Equip.Name = "PetEquipped"
Equip.Value = SaveEquip:GetAsync(player.UserId) or ""
end)
game.Players.PlayerRemoving:Connect(function(player)
local trails = {
}
for _,v in pairs (player.LePet:GetChildren()) do
if game.ReplicatedStorage.SPet[v.Name].Price:FindFirstChild("ID") or game.ReplicatedStorage.SPet[v.Name].Price:FindFirstChild("Group") then
else
trails[v.Name] = v.Value
end
end
Save:SetAsync(player.UserId,trails)
SaveEquip:SetAsync(player.UserId,player.PetEquipped.Value)
end)