I've created a script that by the click of a gui button spawns in a pet placed in ReplicatedStorage, sadly I haven't been able to figure out how to limit the amount of pets you can spawn at once. How can I make it so that whenever the gui button is clicked the pet that you currently have is removed and replaced with the new one?
GUI button:
local target = game.ReplicatedStorage.Dog
script.Parent.MouseButton1Click:connect(function()
if game.Players.LocalPlayer.character:FindFirstChild("Pet") == nil then
local pet = target:Clone()
pet.Parent = game.Players.LocalPlayer.Character
pet.LocalScript.Disabled = false
end
end)
ReplicatedStorage pet:
local char = script.Parent.Parent
local hum = char:FindFirstChild("Humanoid")
local uppertorso = char:FindFirstChild("UpperTorso")
local pet = script.Parent
local maxFloat = 0
local floatInc = 0
local sw = false
local fl = 0
while true do
wait()
if not sw then
fl = fl + floatInc
if fl >= maxFloat then
sw = true
end
else
fl = fl - floatInc
if fl <=-maxFloat then
sw = false
end
end
if pet ~= nil and hum ~= nil and uppertorso ~= nil then
if hum.Health >= 0 then
local cf = uppertorso.CFrame * CFrame.new(2,-2.1,0)
pet.BodyPosition.Position = Vector3.new(cf.x,cf.y,cf.z)
pet.BodyGyro.CFrame = uppertorso.CFrame * CFrame.new(0,0,0)
else
break
end
end
end

2 comments
Thank you so much! Really helpful. Ran into another problem where my pet is invisible to other players and vice versa. Any idea on how I could fix that?
Yep! So these scripts are Local so only the Client can see them, if you want all players to see the Pets you will have to move the cloning function to a Server Script and use a Remote Event from the Client to make the server clone the pet