Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

help with a gun script?

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		local p = Instance.new("Part")
		p.Position = script.Parent.Handle.Position
		p.Parent = game.Workspace
		p.CanCollide = true
		p.Size = Vector3.new(.4,.4,.4)
		p.BrickColor = BrickColor.new("New Yeller")
		p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
		local bf = Instance.new("BodyForce", workspace)
    	bf.Parent = p
    	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end)
end)

I'm trying to make a gun. this part is the script for making the bullet. the velocity is the part I'm having trouble with. i want to make it fire wherever my mouse clicks. the lookvector part sends the part flying correctly, but it wont let me shoot up or down. I've tried using mouse.Hit, mouse.Target, and mouse.Origin. none of them work correctly. how do i fix this?

4 comments

0

You are making multiple connections to the same event every time the tool is equipped. You may want to put your Button1Down event outside of the Equipped event.

0

well, with that, once the player equips the tool and clicks, every time they equip it, a TON of parts get created.

0

Either listen to the Activated event instead OR disconnect the Button1Down event every time the player unequips the tool. (by the way, doesn't solve the problem in the question)

0

wait, the problem isnt in the events, they work fine, i dont see why they need to be changed

1 answer

It looks that you had everything coded right, but you could change up the organization a bit.

script.Parent.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        local p = Instance.new("Part")
        p.Position = script.Parent.Handle.Position
        p.Parent = game.Workspace
        p.CanCollide = true
        p.Size = Vector3.new(.4,.4,.4)
        p.BrickColor = BrickColor.new("New Yeller")
        p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
        local bf = Instance.new("BodyForce", workspace)
        bf.Parent = p
        bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
    end)
end)

First, separate the functions first:

script.Parent.Equipped:connect(function(mouse)
	
end)

 mouse.Button1Down:connect(function()
        local p = Instance.new("Part")
        p.Position = script.Parent.Handle.Position
        p.Parent = game.Workspace
        p.CanCollide = true
        p.Size = Vector3.new(.4,.4,.4)
        p.BrickColor = BrickColor.new("New Yeller")
        p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
        local bf = Instance.new("BodyForce", workspace)
        bf.Parent = p
        bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
end)

Then, put a debounce so that we make sure the gun is still equipped when the player fires:

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
      	 	 local p = Instance.new("Part")
      		 p.Position = script.Parent.Handle.Position
       		 p.Parent = game.Workspace
        	 p.CanCollide = true
       		 p.Size = Vector3.new(.4,.4,.4)
       		 p.BrickColor = BrickColor.new("New Yeller")
        	 p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
       		 local bf = Instance.new("BodyForce", workspace)
        	bf.Parent = p
        	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end
end)

Also, we don't want to create a new part every time we fire right? So let's separate the instance:

 local p = Instance.new("Part")

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
      		 p.Position = script.Parent.Handle.Position
       		 p.Parent = game.Workspace
        	 p.CanCollide = true
       		 p.Size = Vector3.new(.4,.4,.4)
       		 p.BrickColor = BrickColor.new("New Yeller")
        	 p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
       		 local bf = Instance.new("BodyForce", workspace)
        	bf.Parent = p
        	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end
end)

We also want to make the part to the right size before we even decide where to put it and what it does:

local p = Instance.new("Part")
p.CanCollide = true
p.FormFactor = "Custom"		-- don't forget to make it resizable in decimals
p.Size = Vector3.new(.4, .4, .4)
p.BrickColor = BrickColor.new("New Yeller")

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
      		 p.Position = script.Parent.Handle.Position
       		 p.Parent = game.Workspace
        	 p.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
       		 local bf = Instance.new("BodyForce", workspace)
        	bf.Parent = p
        	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end
end)

We also don't want the same bullet to be produced each time do we? Let's clone that:

local p = Instance.new("Part")
p.CanCollide = true
p.FormFactor = "Custom"		-- don't forget to make it resizable in decimals
p.Size = Vector3.new(.4, .4, .4)
p.BrickColor = BrickColor.new("New Yeller")

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
		local C = p.Clone()
      		 C.Position = script.Parent.Handle.Position
       		 C.Parent = game.Workspace
        	 C.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
       		 local bf = Instance.new("BodyForce", workspace)
        	bf.Parent = C
        	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end
end)

We don't want to find the position of the handle, if we are making it go through it:

local p = Instance.new("Part")
p.CanCollide = true
p.FormFactor = "Custom"		-- don't forget to make it resizable in decimals
p.Size = Vector3.new(.4, .4, .4)
p.BrickColor = BrickColor.new("New Yeller")

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
		local C = p.Clone()
      		 C.CFrame = script.Parent.Handle.CFrame
       		 C.Parent = game.Workspace
        	 C.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
       		 local bf = Instance.new("BodyForce", workspace)
        	bf.Parent = C
        	bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()
	end
end)

We also need a body force for each clone before we fire the gun:

local p = Instance.new("Part")
p.CanCollide = true
p.FormFactor = "Custom"		-- don't forget to make it resizable in decimals
p.Size = Vector3.new(.4, .4, .4)
p.BrickColor = BrickColor.new("New Yeller")

local bf = Instance.new("BodyForce", workspace)
bf.Parent = p
bf.force = Vector3.new(0, 196.2, 0) * p:GetMass()

script.Parent.Equipped:connect(function(mouse)
	local equipped = true
end)

 mouse.Button1Down:connect(function()
	if equipped == true then
		local C = p.Clone()
      		 C.CFrame = script.Parent.Handle.CFrame
       		 C.Parent = game.Workspace
        	 C.Velocity = mouse.Origin.p    --script.Parent.Parent.Torso.CFrame.lookVector * 500
	end
end)

Now, I've made a lot of different guns, and I have never used bodyforce to fire it. So, as far as organizing the script, this should help a lot. In regards to the gun shooting like a gun, I suggest to use another bodymover. If you have any questions, I'll do my best to help.

~N

3 comments

0

what other bodymover? and btw, how long did this answer take you?

0

and btw, you made the equipped variable local, which didnt let it be used for the if statement. and you used a '.' instead of a ':' for the :Clone(). after that it works the EXACT SAME AS IT DID BEFORE

0

That's why i said "In regards to the gun shooting like a gun, I suggest to use another bodymover."

This discussion is preserved as it was. New answers, comments, and votes are closed.