Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Using MouseButton1Click in a for loop makes things buggy, is there a way around this? (rep)

Asked by 6 years ago
Edited 6 years ago

When I use a while loop then a for loop then mouseButton1Click, i need to click multiples times before the button even detects that I am clicking the button, its like, I am clicking a button and it is ignoring me until the right time then the functions work, is there a way I can make this instant and less buggy?

repeat wait() until game:GetService("Players").LocalPlayer
local oi = true
local f = script.Parent.InstantOpenAndClose
repeat wait() until game.Players.LocalPlayer.PlayerScripts.gettingMonsters.Done.Value == true
while wait() do
    for _, v in pairs(script.Parent.Parent.Parent.ScrollingFrame:GetChildren()) do
        for _, v in pairs(script.Parent.Parent.Parent.ScrollingFrame:GetChildren()) do
            v.MouseButton1Down:connect(function()       
                f.Value = not f.Value
                script.Parent["Effect_Other_Gui's"].Value = not script.Parent["Effect_Other_Gui's"].Value
                if v then
                    if v:FindFirstChild("Health") then

                    end
                    script.Parent.Parent.ItemName.Text = v:FindFirstChild("Name_Of_Item").Value
                    script.Parent.Parent.Health.Text = "Health: "..v.Health.Value.." (Randomized)"
                    script.Parent.Parent.Sell.DestroyName.Value = v.Name
                end
            end)
        end
    end
end

Don't worry about the stuff inside of the mousebutton1click, I just need a reason why it is so buggy.. Any help would be great, I will also accept the answer and upvote it if the answer is what i needed.

0
I am using the second loop as a "Render step" greatneil80 2647 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

MouseButton1Down and any of it's relatives are events, which means you don't need to constantly set it. The function inside of it will always run unless that function is disconnected from the event. Also you shouldn't be using the same variable name for descending for loops as this will also cause bugs. I want to give you an answer on how to fix this script, but I need to know the intention behind this script.

If you just want new children to have the function connected to their mouseButton1Down event then just use their parent's ChildAdded event.

The main reason it's probably buggy though is because the mouseButton1Down event is being set every frame and not being disconnected at all.

Edit:

local ply = game.Players.LocalPlayer    --No need to wait for LocalPlayer as it loads before pretty much anything client-side.
local oi = true
local f = script.Parent.InstantOpenAndClose
local scrollFrame = script.Parent.Parent.Parent.ScrollingFrame
local effectOthers = script.Parent:FindFirstChild("Effect_Other_Gui's")
local events = {}

local function activateGui()
    for i,v in pairs(scrollFrame:GetChildren()) do  --? Should be avoided
        events[v] = v.MouseButton1Down:connect(function()
            f.Value = not f.Value
            effectOthers.Value = not effectOthers.Value

            script.Parent.Parent.ItemName.Text = v:FindFirstChild("Name_Of_Item").Value
            script.Parent.Parent.Health.Text = "Health"..tostring(v.Health.Value).." (Randomized)"
            script.Parent.Parent.Sell.DestroyName.Value = v.Name
        end)
    end
end
local function deactivateGui()
    for i,v in pairs(scrollFrame:GetChildren()) do
        if events[v] then
            v:disconnect()
        end
    end
end

ply.PlayerScripts.gettingMonsters.Done.Changed:connect(function(val)
    if val then
        activateGui()
    else
        deactivateGui()
    end
end)

Let me know if this works or what errors you get.

0
I need this script, it is an essential part of my game, it opens a frame, labels the name of an item, determines the health of the item (Randomized) and the sell price. The loop is going through multiple buttons and making sure each is clicked but it seems to not work. greatneil80 2647 — 6y
0
You don't need to do that in Roblox or any good game engine. Events fire the function they are connected to when they are triggered so you only need to set them once per function. Meltdown81 309 — 6y
Ad

Answer this question