My gun script goal is to send a bullet flying at a target but when I play it in-game not in studio the bullets fly in the wrong direction. Can anyone fix the code of this? Thanks in advanced!
originalAmmo = script.Parent.Configure.Ammo.Value
function computeDirection(vec)
local lenSquared = vec.magnitude^2
local invSqrt = 1 / math.sqrt(lenSquared)
return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end
local isActive = false
local canfire=true
local playerName
function fire(v)
if canfire==true then
script.Parent.Handle.Fire2:Play()
script.Parent.Handle.Fire:Play()
script.Parent.Configure.Ammo.Value = script.Parent.Configure.Ammo.Value - 1
local dir = v - script.Parent["Handle"].Position
dir = computeDirection(dir)
local pos = script.Parent["Handle"].Position + (dir * 8)
local p = Instance.new("Part")
p.Shape = "Cylinder"
p.Anchored = true
p.Name = "Projectile"
p.Material = "Neon"
p.Color = Color3.new(255,255,255)
p.BrickColor = BrickColor.new("Institutional white")
p.Size = Vector3.new(.5, 2, .5)
p.Orientation = dir
p.CanCollide = false
local s = script.Parent["ProjectileScript"]:Clone()
s.Disabled = false
s.Parent = p
p.Parent = game.Workspace
s.Value.Value = script.Parent.Configure.Damage.Value
s.Headshot.Value = script.Parent.Configure.HeadshotDamage.Value
p.Parent = workspace
p.Anchored = false
p.Position =script.Parent.Flash.Position
p.Velocity = (playerName.Character.Head.Position - v).unit * -400 + Vector3.new(math.random(-2,2),math.random(-2,2),math.random(-2,2))
local upforce = Instance.new("BodyForce")
upforce.Parent = p
upforce.force = Vector3.new(0, p:GetMass() * script.Parent.Configure.Speed.Value , 0)
script.Parent.Flash.Light.Light.Visible = true
wait(0.05)
script.Parent.Flash.Light.Light.Visible = false
wait(script.Parent.Configure.ClickTick.Value - 0.05)
canfire=true
end
end
local canrel = true
local canAuto = true
if script.Parent.Configure.Auto.Value~=true then
script.Parent.Activated:Connect(function()
if script.Parent.Enabled == true then
if script.Parent.Configure.Ammo.Value > 0 or script.Parent.Configure.InfiniteMagSize then
fire(playerName.Character.Humanoid.TargetPoint)
script.Parent.Configure.Ammo.Value = originalAmmo
elseif canrel==true then
canrel = false
script.Parent.Handle.Reload:Play()
wait(script.Parent.Configure.ReloadTime)
script.Parent.Configure.Ammo.Value = originalAmmo
canrel = true
end
end
isActive = true
end)
else
script.Parent.Activated:Connect(function()
if canAuto==true then
repeat
canAuto=false
if canfire~=false then
if script.Parent.Configure.Ammo.Value > 0 or script.Parent.Configure.InfiniteMagSize then
fire(playerName.Character.Humanoid.TargetPoint)
script.Parent.Configure.Ammo.Value = originalAmmo
wait(script.Parent.Configure.ClickTick.Value)
elseif canrel==true then
canrel = false
script.Parent.Handle.Reload:Play()
wait(script.Parent.Configure.ReloadTime)
script.Parent.Configure.Ammo.Value = originalAmmo
canrel = true
end
end
until script.MouseDown.Value==false or script.Parent.Configure.Ammo.Value<=0
canAuto=true
end
end)
end
script.Parent.Unequipped:Connect(function()
isActive = false
end)

3 comments
Thank you! How do you verify a front surface?
Instead of making a new part you could clone your muzzle by example and put the front surface at the opposite side of where you want the projectile to go and to verify the surface go in the propreties of your part and you will find it pretty fast
You could also use raycasting like im doing for my own framework