Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I avoid players spawning more than one pet?

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

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use :WaitForChild() to make sure something exists before using / changing it

Use Activated instead of MouseButton1Click since Activated works for all devices, not just computers

Use :GetService() to retrieve Players and ReplicatedStorage

ISSUES

connect is deprecated in favor of Connect

The player's character is defined using a capitalized "C" (Character instead of character)

You can remove pets using the :Destroy() function after checking that there is one using :FindFirstChild(). If there is not, the pet is cloned anyway.

To prevent players from spawning more than one pet, make sure that the Clone will always have the same Name as the one you are checking for to use :Destroy()

REVISED TEXTBUTTON / IMAGEBUTTON LOCAL SCRIPT

local target = game:GetService("ReplicatedStorage"):WaitForChild("Dog")
local char = game:GetService("Players").LocalPlayer.Character

script.Parent.Activated:Connect(function()
	if char:FindFirstChild("Pet") ~= nil then
		char:WaitForChild("Pet"):Destroy()
	end
	
	local pet = target:Clone()
	pet.Name = "Pet"
	pet.Parent = char
	pet:WaitForChild("LocalScript").Disabled = false
end)

REVISED PET LOCAL SCRIPT

local pet = script.Parent
local char = pet.Parent
local hum = char:FindFirstChild("Humanoid")
local uppertorso = char:FindFirstChild("UpperTorso")

local maxFloat = 0
local floatInc = 0
local fl = 0
local sw = false

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:WaitForChild("BodyPosition").Position = Vector3.new(cf.X, cf.Y, cf.Z)
			pet:WaitForChild("BodyGyro").CFrame = uppertorso.CFrame * CFrame.new(0, 0, 0)
		else
			break
		end
	end
end

2 comments

0

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?

0

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

This discussion is preserved as it was. New answers, comments, and votes are closed.