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
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
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
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
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"
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
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"
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
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"
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
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"
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
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
4 comments
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.
well, with that, once the player equips the tool and clicks, every time they equip it, a TON of parts get created.
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)
wait, the problem isnt in the events, they work fine, i dont see why they need to be changed