Hi, I recently made a gun system to my game, but I can’t fix a problem; bullets sometimes are not going directly to mouse position
Server Script(only the main part)
local debounce = false
local firerate = 0.2
local damage = 25
local maxAmmo = 8
local ammo = 8
local reloading = false
local equipped = false
script.Parent.Equipped:Connect(function()
equipped = true
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
-- This function gets the mouse position from local script --
script.Parent.GetMousePos.OnServerEvent:Connect(function(player, mouse)
script.Parent.Value.Value = mouse
if equipped == true then
player.PlayerGui.AmmoGui.Ammo.Text = ammo
player.PlayerGui.AmmoGui.GunName.Text = script.Parent.Name
end
end)
-- Damage indicator I added --
local function damageIndicatorPos(object)
for i = 1, 25, 1 do
object.ExtentsOffset = object.ExtentsOffset + Vector3.new(0,0.06,0)
object.TextLabel.TextTransparency = object.TextLabel.TextTransparency + 0.04
object.TextLabel.TextStrokeTransparency = object.TextLabel.TextStrokeTransparency + 0.04
wait()
end
object:Destroy()
end
-- Main script --
script.Parent.Activated:Connect(function()
if reloading == false then
if debounce == false then
if ammo > 0 then
ammo = ammo - 1
print(ammo)
script.Parent.ShotEffectPos.Smoke.Enabled = true
script.Parent.BulletPos.Shot:Play()
script.Parent.SlideA.Transparency = 1
script.Parent.SlideB.Transparency = 0
local bullet = game.ServerStorage.Bullet:Clone()
local bullet2 = game.ServerStorage.ChamberBullet:Clone()
bullet2.Parent = workspace
bullet2.CFrame = script.Parent.ChamberBulletPos.CFrame
bullet2.Velocity = script.Parent.ChamberBulletPos.CFrame.LookVector * 50
bullet.Parent = workspace
bullet.CFrame = CFrame.new(script.Parent.BulletPos.Position, script.Parent.Value.Value.Position)
bullet.Velocity = script.Parent.Value.Value.LookVector * 650
bullet.CanCollide = false
bullet.Touched:Connect(function(hit)
if hit.Parent then
if hit.Parent ~= script.Parent and hit.Parent ~= script.Parent.Parent then
bullet:Destroy()
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Head") then
hit.Parent.Humanoid:TakeDamage(damage)
bullet:Destroy()
local damageIndicator = game.ServerStorage.BillboardGui:Clone()
damageIndicator.Adornee = hit.Parent.Head
damageIndicator.TextLabel.Text = damage
damageIndicator.Parent = hit.Parent
damageIndicatorPos(damageIndicator)
end
end
end
end)
debounce = true
wait(0.05)
script.Parent.SlideA.Transparency = 0
script.Parent.SlideB.Transparency = 1
wait(firerate)
script.Parent.ShotEffectPos.Smoke.Enabled = false
debounce = false
end
end
end
end)