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

How to make it where if you have already clicked a part it wont run a part again?

Asked by 1 year ago
Edited by Rare_tendo 1 year ago

So I am trying to make a part and if you click then it will add a folder with an int value in it but if i click it again then it will put another folder in it does anybody know how to stop this?

Here is my script:

local Tool2 = script.Parent
local Tool2Damage = Tool2.Folder.Tool2Damage
Tool2Damage.Value = 3

local MinePartsFolder = game.Workspace.MinePartsFolder -- The folder where all of the parts that you can mine are in

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Equipped:Connect(function()
    mouse.Button1Down:Connect(function()
        if mouse.Target and mouse.Hit then
            if mouse.Target.Name == "MinePart" then
                local HealthFolder = Instance.new("Folder", mouse.Target) -- Adding the folder into the part that you clicked
                HealthFolder.Name = "HealthFolder"

                local Health = Instance.new("IntValue", HealthFolder) -- Adding the int value into the folder
                Health.Name = "Health"
                Health.Value = 15
            end
        end
    end)
end)
0
can you make your code in a code block? fire_yusuf 0 — 1y
0
edited the question to include the codeblock and indented the code properly Rare_tendo 3000 — 1y

3 answers

Log in to vote
1
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
1 year ago

You can check if the folder already exists in the part by using FindFirstChild, which takes the child to find as a string as an argument and will return the instance if found or it will return nil. If the folder does exist, then we won't add it, otherwise we will add the folder. You can do this with a simple if statement:

if (mouse.Target.Name == "MinePart") then
    if (not mouse.Target:FindFirstChild("HealthFolder") then
        -- proceed to adding the folder to the part, as the folder does not exist
    end
end

A few other things worth mentioning:

  • You should avoid using the 2nd argument of Instance.new, which is the parent. This has been shown to cause performance issues, as explained in this official PSA
  • Don't nest connections if you're not going to disconnect them: Here you are nesting a connection to the Button1Down event of the mouse in the Equipped event of the tool. This is bad because you once you re-equip the tool, another connection gets made to the Button1Down event and it will keep on adding up the more times you re-equip your tool. This will lead to multiple listeners listening for the same thing, which could cause unintended results. In your case, it would add more and more folders each time you click and re-equip your weapon. Consider using the Activated event instead.
  • With regard to posting questions, please use a codeblock and indent your code properly. This makes it more readable for people who are willing to help you. Above the question prompt, you should see a Lua icon which inserts two sequences of tildes. Whatever is between those will be placed in a codeblock
0
Thanks iamnotcool235_s 27 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Hi. You could create a variable at the top called something like

IsActive = 1

Then put the Click script inside an if statement, then increment IsActive, so it no longer equals 1.

if IsActive == 1 then

    Insert "Click script" here
    IsActive += 1

end

So, before you click the part, IsActive equals 1, meaning the contents of the if statement will run. Then, after you click the part, IsActive no longer equals 1, meaning the contents of the if statement will be ignored and the Click script will not run.

Also, if you were wondering,

IsActive += 1

is the same as writing:

IsActive = IsActive + 1

It's just a shorter and cleaner way to do the same thing. This type of shorthand is called a Compound Assignment Operator which you can see more examples of if you scroll to the bottom of this page.

0
This would seem to prevent any folders being added to literally any other part once you initially add click a part and add a folder to it, which I don't think is what the OP wants. Also, if you're making something like a debounce, consider using a boolean variable Rare_tendo 3000 — 1y
Log in to vote
0
Answered by 1 year ago

Check it using FindFirstChild. ~~~~~~~~~~~~~~~~~ local Tool2 = script.Parent local Tool2Damage = Tool2.Folder.Tool2Damage Tool2Damage.Value = 3

local MinePartsFolder = game.Workspace.MinePartsFolder -- The folder where all of the parts that you can mine are in

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
 if not mouse.Target:FindFirstChild("HealthFolder") then

script.Parent.Equipped:Connect(function()
    mouse.Button1Down:Connect(function()
    if mouse.Target and mouse.Hit then
        if mouse.Target.Name == "MinePart" then
          local HealthFolder = Instance.new("Folder", mouse.Target) -- Adding the folder into the part that you clicked
            HealthFolder.Name = "HealthFolder"

            local Health = Instance.new("IntValue", HealthFolder) -- Adding the int value into the folder
            Health.Name = "Health"
            Health.Value = 15
            end
        end

end end) end)

~~~~~~~~~~~~~~~~~

Answer this question