You can write your topic however you want, but you need to answer these questions:
I want to create a fully funcional pet system where you can equip and unequip and delete pets.
So im trying to assign the name of the pet to each frame but it returns like this when I click the pet frame and print it as you see it shows multiple pet names each frame and I dont know how to fix this.
Ive tried to debug or find issues on it but I couldnt.
Here is my code with comments
for i, v in pairs(game.ReplicatedStorage.PetFolder:GetChildren()) do
-- Loop through all children of the "PetFolder" in ReplicatedStorage.
local PetFrame = script.Parent.Parent["Pet Frame"]:Clone()
-- Clone the "Pet Frame" from the parent of the script and store it in "PetFrame" variable.
PetFrame.Parent = script.Parent
-- Set the parent of the cloned "PetFrame" to be the parent of the script.
local Rarity = v:FindFirstChild("Rarity")
-- Find the child named "Rarity" in the current pet object.
local petName = v.Name
-- Get the name of the current pet and store it in the "petName" variable.
print(petName)
-- Print the name of the current pet to the output console.
PetFrame.Visible = true
-- Make the "PetFrame" visible.
PetFrame.Name = petName
-- Set the name of the "PetFrame" to the name of the current pet.
local Module3D = require(game.ReplicatedStorage:WaitForChild("Module3D"))
-- Require the "Module3D" module from ReplicatedStorage.
local Model3D = Module3D:Attach3D(PetFrame, v:Clone())
-- Create a 3D model of the current pet and attach it to the "PetFrame".
Model3D:SetDepthMultiplier(1.2)
-- Set the depth multiplier of the 3D model.
Model3D.Camera.FieldOfView = 5
-- Set the field of view of the 3D model camera to 5.
Model3D.Visible = true
-- Make the 3D model visible.
game:GetService("RunService").RenderStepped:Connect(function()
-- Connect a function to the RenderStepped event, which runs on every frame update.
Model3D:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
-- Set the CFrame of the 3D model to rotate it continuously on the Y-axis with a slight tilt on the X-axis.
end)
PetFrame.InputBegan:Connect(function(input)
-- Connect a function to the InputBegan event of the "PetFrame".
if input.UserInputType == Enum.UserInputType.MouseButton1 then
-- Check if the input is a left mouse button click.
local Info = script.Parent.Parent.Parent.Information
-- Get the "Information" object from the parent of the parent of the script and store it in "Info" variable.
Info.ItemName.Text = petName
-- Set the text of "ItemName" in the "Information" object to the name of the current pet.
Info.Description.Text = tostring("Rarity: " .. Rarity.Value)
-- Set the text of "Description" in the "Information" object to the rarity value of the current pet.
local Pet = game.ReplicatedStorage.PetFolder:FindFirstChild(petName):Clone()
-- Clone the current pet object from the "PetFolder" in ReplicatedStorage and store it in "Pet" variable.
local Model3d = Module3D:Attach3D(Info.ImageContainer.ViewportFrame, Pet)
-- Create a 3D model of the pet and attach it to the "ViewportFrame" in the "ImageContainer" of the "Information" object.
Model3d:SetDepthMultiplier(1.2)
-- Set the depth multiplier of the 3D model.
Model3d.Camera.FieldOfView = 5
-- Set the field of view of the 3D model camera to 5.
Model3d.Visible = true
-- Make the 3D model visible.
game:GetService("RunService").RenderStepped:Connect(function()
-- Connect a function to the RenderStepped event, which runs on every frame update.
Model3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
-- Set the CFrame of the 3D model to rotate it continuously on the Y-axis with a slight tilt on the X-axis.
end)
Info.EquipButton.MouseButton1Click:Connect(function()
-- Connect a function to the MouseButton1Click event of the "EquipButton" in the "Information" object.
print(petName)
-- Print the name of the current pet to the output console when the "EquipButton" is clicked.
end)
end
end)
end
If you found the issue tell me how I could revise the script and fix the issue as it seems everything I tried isnt working.
Info.EquipButton.MouseButton1Click:Connect(function()
-- Connect a function to the MouseButton1Click event of the "EquipButton" in the "Information" object.
print(petName)
-- Print the name of the current pet to the output console when the "EquipButton" is clicked.
end)
This function is overlapping the button, so when you click equip, multiple events are called.
Try creating a global variable called local conn and leave it empty.
Then, assign the equip event to the variable, like this:
conn = Info.EquipButton.MouseButton1Click:Connect(function()
-- Connect a function to the MouseButton1Click event of the "EquipButton" in the "Information" object.
print(petName)
-- Print the name of the current pet to the output console when the "EquipButton" is clicked.
end)
When you click the pet frame, call conn:Disconnect() before redefining the equip event.
This should solve the problem.
local Info = script.Parent.Parent.Parent.Information
for i, v in pairs(game.ReplicatedStorage.PetFolder:GetChildren()) do
local PetFrame = script.Parent.Parent["Pet Frame"]:Clone()
PetFrame.Parent = script.Parent
local Rarity = v:FindFirstChild("Rarity")
local petName = v.Name
print(petName)
PetFrame.Visible = true
PetFrame.Name = petName
local Module3D = require(game.ReplicatedStorage:WaitForChild("Module3D"))
local Model3D = Module3D:Attach3D(PetFrame, v:Clone())
Model3D:SetDepthMultiplier(1.2)
Model3D.Camera.FieldOfView = 5
Model3D.Visible = true
game:GetService("RunService").RenderStepped:Connect(function()
Model3D:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
end)
local conn = Info.EquipButton.MouseButton1Click:Connect(function()
-- Connect a function to the MouseButton1Click event of the "EquipButton" in the "Information" object.
print(petName)
-- Print the name of the current pet to the output console when the "EquipButton" is clicked.
end)
PetFrame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
conn:Disconnect()
Info.ItemName.Text = petName
Info.Description.Text = tostring("Rarity: " .. Rarity.Value)
local Pet = game.ReplicatedStorage.PetFolder:FindFirstChild(petName):Clone()
local Model3d = Module3D:Attach3D(Info.ImageContainer.ViewportFrame, Pet)
Model3d:SetDepthMultiplier(1.2)
Model3d.Camera.FieldOfView = 5
Model3d.Visible = true
game:GetService("RunService").RenderStepped:Connect(function()
Model3d:SetCFrame(CFrame.Angles(0, tick() % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10), 0, 0))
end)
end
end)
end
the easiest method for me to connect and disconnect events is simple
local connection
function functionthing()
--code
connection:Disconnect()
connection = event:Connect(functionthing)
end
connection = event:Connect(functionthing)