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)
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:
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.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.
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)
~~~~~~~~~~~~~~~~~