-
What do you want to achieve? I want to fix 2 problems in my script.
-
What is the issue? When i click on a button it fires an event multiple times and it clones a different character in the viewport frame.
-
What solutions have you tried so far? Debugging, Devforum, remaking the script and chatgpt.
So basically my script is a handler script, that automatically makes frames depending on the items that are in a folder in replicatedStorage and it makes the function. The problem is: When clicking on a button the event fires multiple times and there are multiple characters cloning in the viewport frame.
local template = script.Template:Clone()
template.Parent = charactersFrame.ScrollingFrame
local name = folder.Name
local equipped = folder.Equipped
local rarity = folder.Rarity
local camera = Instance.new("Camera", template.ViewportFrame)
template.ViewportFrame.CurrentCamera = camera
local characterModel = game.ReplicatedStorage.Characters[name]:Clone()
characterModel.Parent = template.ViewportFrame
local hrp = characterModel.PrimaryPart
camera.CFrame = CFrame.new(hrp.Position + hrp.CFrame.LookVector * 7, hrp.Position)
template.ViewportFrame.IconName.Text = name
if raritiesColors[rarity.Value] then
template.ViewportFrame.IconName.TextColor3 = raritiesColors[rarity.Value]
end
if equipped.Value == true then
template.MainButton.Text = "Equipped"
else
template.MainButton.Text = "Equip"
end
template.MainButton.MouseButton1Click:Connect(function()
if template.MainButton.Text == "Equip" then
local success = charEvents.Action:InvokeServer("Equip", name)
if success == "Successful Equip" then
for _, frame in pairs(charactersFrame.ScrollingFrame:GetChildren()) do
if frame:IsA("Frame") then
frame.MainButton.Text = "Equip"
end
end
wearSound:Play()
template.MainButton.Text = "Equipped"
else
local errorTemplate = script.ErrorTemplate:Clone()
errorTemplate.ErrorMessage.Text = success
if ui:FindFirstChild("Error") then
ui:FindFirstChild("Error"):Destroy()
end
errorTemplate.Name = "Error"
errorTemplate.Parent = ui
wait(2)
errorTemplate:Destroy()
end
else
clickSound:Play()
end
end)
template.Name = name
if template.Name == "Noobie" then
template.LayoutOrder = 0
else
template.LayoutOrder = raritiesOrder[rarity.Value]
end
end
I’m not askin a full script, i just need an explanation and a possible way to fix my problem. Thanks for responding!