i am making a script that fires a bullet it works but it is sometimes off center i am using a sniper so accuracy is very important

--locals
local mouse = game.Players.LocalPlayer:GetMouse()
local plr  = game.Players.LocalPlayer
local plr = game.Workspace:WaitForChild(plr.Name)
local myhum = plr:WaitForChild("Humanoid")
local config = script.Parent.Parent:WaitForChild("Config")
local debounce = true
local tool = script.Parent.Parent
--creating bullet
local BaseShot = Instance.new("Part")
BaseShot.Name = "Effect"
BaseShot.BrickColor = BrickColor.new("New Yeller")
BaseShot.Material = Enum.Material.Plastic
BaseShot.Shape = Enum.PartType.Block
BaseShot.TopSurface = Enum.SurfaceType.Smooth
BaseShot.BottomSurface = Enum.SurfaceType.Smooth
BaseShot.FormFactor = Enum.FormFactor.Custom
BaseShot.Size = Vector3.new(0.2, 0.2, 3)
BaseShot.CanCollide = false
BaseShot.Locked = true
BaseShot.Parent = game.ReplicatedStorage
--main script
script.Parent.Parent.Activated:Connect(function()
    if myhum.Health > 0 and not config.reloading.Value and debounce  and config.CurrentAmmo.Value > 0 then
		config.CurrentAmmo.Value = config.CurrentAmmo.Value - 1
		debounce = false
		script.Parent.Parent.Handle.shoot:Play()        
        local barrelCFrame = script.Parent.Parent.barrel.CFrame
		local FiringPoint = barrelCFrame.p + barrelCFrame:VectorToWorldSpace(Vector3.new(1,0,2))
		local mousep = mouse.Hit.p
        local ShotCFrame = CFrame.new(FiringPoint,mousep)

        local bulletclone = BaseShot:Clone()
        game:GetService("Debris"):AddItem(bulletclone,3)
        bulletclone.Parent = game.Workspace
        bulletclone.CFrame = ShotCFrame + (ShotCFrame.lookVector * (BaseShot.Size.Z / 2))
        local bodyv = Instance.new("BodyVelocity")
        bodyv.velocity = ShotCFrame.lookVector * 1000
        bodyv.Parent = bulletclone
        bulletclone.Touched:Connect(function(hit)
			bulletclone:Destroy()
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
				--damage
				if hit.Name == "Head" then
					script.Parent.Parent.Handle.HeadShot:Play()
					game.Players.LocalPlayer.PlayerGui.ScreenGui.HeadShot.ImageTransparency = 0
					game.ReplicatedStorage:WaitForChild("Damage"):FireServer(hit.Parent.Humanoid,100)
					wait(0.3)
					game.Players.LocalPlayer.PlayerGui.ScreenGui.HeadShot.ImageTransparency = 1
				else
					print(hit.Name)
					script.Parent.Parent.Handle.BodyShot:Play()
					game.Players.LocalPlayer.PlayerGui.ScreenGui.BodyShot.ImageTransparency = 0
					game.ReplicatedStorage:WaitForChild("Damage"):FireServer(hit.Parent.Humanoid,"50")
					wait(0.3)
					game.Players.LocalPlayer.PlayerGui.ScreenGui.BodyShot.ImageTransparency = 1
				end
			end
        end)
        wait(2)
        debounce = true
    end
end)

also note that it is always off center in the same place the upper left side is there any way to fix this?

4 comments

0

Are you trying to create a raycast gun?

0

no

0

another important thing to note when the lookvector on line 38 is 100 it is very accurate but it make the bullet be very slow

0

i just made it be 500 the accuracy is great but it is a tad on the slow side but again i am shooting it from one side to the other so the player will probably expect this