Shooting script isnt working properly

So I’m trying to make a shooting script where when the player holds down the mouse button bullets shoot out. And the when its listed up the bullets stop shooting. So ive made a script though it wont work and im not sure why. There are no errors in the output and this is a local script located in a tool in starterpack. No handle is required

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local Tool = script.Parent --make sure this is a Tool object
local blue = true

local canshoot = true


Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		
		if Mouse.Button1Down then
			canshoot = true
		end
		if Mouse.Button1Up then
			canshoot = false
		end

		
		if canshoot == true then
		while blue do
			wait(.2)

		print("Button1Down")
			local sound = script.Parent.Stealer
			sound:Play()
			print('Activation works')
			local ball = Instance.new("Part")
			ball.Shape = Enum.PartType.Ball
			ball.Color = Color3.new(0.333333, 1, 0.498039)
			ball.Size = Vector3.new(.7,.7,.7)
			ball.CanCollide = false
			ball.Material = Enum.Material.Neon
			ball.Parent = root
			local p = Instance.new("ParticleEmitter")
			p.Texture = 'http://www.roblox.com/asset/?id=290556384'
			p.Parent = ball
			p.Size = NumberSequence.new(.2,.2)
			p.Speed = NumberRange.new(0,0)
			p.Lifetime = NumberRange.new(.2,.2)
			p.Color = ColorSequence.new(Color3.new(0, 1, 0),Color3.new(0, 1, 0.498039))


			local newCFrame = root.CFrame
			local cf = ball.CFrame

			ball.CFrame = newCFrame


			local Velocity = Instance.new("BodyVelocity")

			Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
			Velocity.Velocity = ball.CFrame.lookVector * 30


			Velocity.Parent = ball

			ball.Touched:connect(function(hit) 
				if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
					local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
					humanoid1:TakeDamage(20)
					local cool = game.ReplicatedStorage.Cool
					cool:FireServer(player)





					ball.CanCollide = false
					ball.CanTouch = false
					ball.Transparency = 1
					p:Destroy()




				end
			end)
			ball.Touched:connect(function(hit) 
				if hit:IsA("Part")  and hit.Parent ~= player.Character then


					ball.CanCollide = false
					ball.CanTouch = false
					ball.Transparency = 1
					p:Destroy()



				end

			end)
		end
		end
end)
end)

Mouse with tool.Equipped function is deprecated. use player:GetMouse() instead. Also, I would recommend moving the Mouse.Button1Down function outside of the Equipped function unless you disconnect the function after Unequipping. Here’s a fixed version.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local Tool = script.Parent --make sure this is a Tool object
local blue = true
local Mouse = player:GetMouse()
local connection --this will be the connection to disconnect.
local canshoot = true


Tool.Equipped:Connect(function()
	connection = Mouse.Button1Down:Connect(function()
		
		if Mouse.Button1Down then
			canshoot = true
		end
		if Mouse.Button1Up then
			canshoot = false
		end

		if canshoot == true then
			while blue do
				wait(.2)
				print("Button1Down")
				local sound = script.Parent.Stealer
				sound:Play()
				print('Activation works')
				local ball = Instance.new("Part")
				ball.Shape = Enum.PartType.Ball
				ball.Color = Color3.new(0.333333, 1, 0.498039)
				ball.Size = Vector3.new(.7,.7,.7)
				ball.CanCollide = false
				ball.Material = Enum.Material.Neon
				ball.Parent = root
				local p = Instance.new("ParticleEmitter")
				p.Texture = 'http://www.roblox.com/asset/?id=290556384'
				p.Parent = ball
				p.Size = NumberSequence.new(.2,.2)
				p.Speed = NumberRange.new(0,0)
				p.Lifetime = NumberRange.new(.2,.2)
				p.Color = ColorSequence.new(Color3.new(0, 1, 0),Color3.new(0, 1, 0.498039))

				local newCFrame = root.CFrame
				local cf = ball.CFrame

				ball.CFrame = newCFrame

				local Velocity = Instance.new("BodyVelocity")
				Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
				Velocity.Velocity = ball.CFrame.lookVector * 30
				Velocity.Parent = ball

				ball.Touched:connect(function(hit) 
					if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
						local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
						humanoid1:TakeDamage(20)
						local cool = game.ReplicatedStorage.Cool
						cool:FireServer(player)

						ball.CanCollide = false
						ball.CanTouch = false
						ball.Transparency = 1
						p:Destroy()
					end
				end)
				ball.Touched:connect(function(hit) 
					if hit:IsA("Part")  and hit.Parent ~= player.Character then
						ball.CanCollide = false
						ball.CanTouch = false
						ball.Transparency = 1
						p:Destroy()
					end
				end)
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	connection:Disconnect()
end)

Those are events not functions.

I think this is what you want to do:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local Tool = script.Parent --make sure this is a Tool object
local blue = true

local canshoot = true


Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		canshoot = true
		while blue do
			wait(.2)
			if canshoot == true then

				print("Button1Down")
				local sound = script.Parent.Stealer
				sound:Play()
				print('Activation works')
				local ball = Instance.new("Part")
				ball.Shape = Enum.PartType.Ball
				ball.Color = Color3.new(0.333333, 1, 0.498039)
				ball.Size = Vector3.new(.7,.7,.7)
				ball.CanCollide = false
				ball.Material = Enum.Material.Neon
				ball.Parent = root
				local p = Instance.new("ParticleEmitter")
				p.Texture = 'http://www.roblox.com/asset/?id=290556384'
				p.Parent = ball
				p.Size = NumberSequence.new(.2,.2)
				p.Speed = NumberRange.new(0,0)
				p.Lifetime = NumberRange.new(.2,.2)
				p.Color = ColorSequence.new(Color3.new(0, 1, 0),Color3.new(0, 1, 0.498039))


				local newCFrame = root.CFrame
				local cf = ball.CFrame

				ball.CFrame = newCFrame


				local Velocity = Instance.new("BodyVelocity")

				Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
				Velocity.Velocity = ball.CFrame.lookVector * 30


				Velocity.Parent = ball

				ball.Touched:connect(function(hit) 
					if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
						local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
						humanoid1:TakeDamage(20)
						local cool = game.ReplicatedStorage.Cool
						cool:FireServer(player)





						ball.CanCollide = false
						ball.CanTouch = false
						ball.Transparency = 1
						p:Destroy()




					end
				end)
				ball.Touched:connect(function(hit) 
					if hit:IsA("Part")  and hit.Parent ~= player.Character then


						ball.CanCollide = false
						ball.CanTouch = false
						ball.Transparency = 1
						p:Destroy()



					end

				end)
			end
		end
	end)
	Mouse.Button1Up:Connect(function()
		canshoot = false
	end)
end)
1 Like

It works! Thanks.


1 Like