When Player holds on a part something happens [How to]

Hello, I am trying to make it so when the player is holding on a part the part emmits particles, but when the player stops holding the particles dissapear


local Debris = game:GetService("Debris")

local KeyService = game:GetService("UserInputService")



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

local selectingBox = Instance.new("SelectionBox", workspace)
local isholding = false
game:GetService("RunService").RenderStepped:Connect(function()

	local target = mouse.Target
	if target then
		
			
		
		

				selectingBox.Adornee = target
				
				
					
				KeyService.InputBegan:Connect(function(io,gp)
					
					
					if gp then return end
					
					
					if io.UserInputType == Enum.UserInputType.MouseButton1 then
						isholding = true
					end)
						
				KeyService.InputEnded:Connect(function(io,gp)
					if gp then return end
					if io.UserInputType == Enum.UserInputType.MouseButton1 then
						isholding = false
					end
					
					
				end)
				
				if isholding == true then
		 game:GetService("ReplicatedStorage"):WaitForChild("Particle"):WaitForChild("BreakingParticle"):Clone().Parent = target
					if isholding == false then
						if target:FindFirstChild("BreakingParticle") then
							target:FindFirstChild("BreakingParticle"):Destroy()
						end	
					end
					
				end	
						
					
				
				

		else
			selectingBox.Adornee = nil
			end
		end
	end
end)
1 Like

Don’t create connections inside of loops unless you are intending to further disconnect them. Each connection works in their own environment and has no regard for ending loops & etc.

local players = game:GetService("Players")
local debris = game:GetService("Debris")
local repstore = game:GetService("ReplicatedStorage")
local inputservice = game:GetService("UserInputService")
local runservice = game:GetService("RunService")
local guiservice = game:GetService("GuiService")

local repparticle = repstore:WaitForChild("Particle")
local particlebreak = repparticle:WaitForChild("BreakParticle")

local localplayer = players.LocalPlayer
local playermouse = localplayer:GetMouse()
local selectionbox = Instance.new("SelectionBox")
selectionbox.Parent = localplayer:WaitForChild("PlayerGui")

local target, object, particle
local held = false

inputservice.InputBegan:Connect(function(input)
	if not guiservice.MenuIsOpen and not inputservice:GetFocusedTextBox() and input.UserInputState.Name == 'Begin' and input.UserInputType.Name == 'MouseButton1' and typeof(target) == 'Instance' then
		held = true
		object = target
	end
end)

inputservice.InputEnded:Connect(function(input)
	if input.UserInputType.Name == 'MouseButton1' then
		held = false
		object = nil
	end
end)

runservice.Stepped:Connect(function()
	target = playermouse.Target
	if typeof(target) == 'Instance' then
		selectionbox.Adornee = target
		if typeof(particle) ~= 'Instance' then
			particle = particlebreak:Clone()
		end
		particle.Parent = target
	elseif typeof(particle) == 'Instance' then
		particle:Destroy()
	end
end)
1 Like