Gun Script Not Working

I am trying to make a gun that when it shoots, it shoots a bullet that slowly falls towards the ground that farther it goes. Here is what I have so far but it does not dip, and when turning, it acts strange. You just put this script and parent it to a tool and it will function.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local Run = game:GetService("RunService")

local function CalculateLandingPoint(start,velocity,char)
	velocity = Vector3.new(0,0,velocity)
	local direction = start.LookVector * velocity
	local startTime = tick()
	local spos = start.Position
	while wait() do
		if tick()-startTime >= 3 then print("Unable to find object") break end
		local dt = Run.RenderStepped:Wait()
		local ray = Ray.new(spos,direction)
		local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})
		if hit then
			print(pos)
			break
		else
			velocity = velocity-(Vector3.new(0,workspace.Gravity,0) * dt)
			direction = start.LookVector * velocity
			spos = pos
			local part = Instance.new("Part")
			part.Size = Vector3.new(1,1,1)
			part.Anchored = true
			part.CanCollide = false
			part.Position = spos
			part.Parent = workspace
		end
	end
end

mouse.Button1Down:connect(function()
	local RunService = game:GetService("RunService")

	CalculateLandingPoint(plr.Character.PrimaryPart.CFrame,2,plr.Character)
end)```

Hi, I rewrote some of the code for you.

local LocalPlayer = game.Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

local RunService = game:GetService'RunService'

local function CalculateLandingPoint(Origin, Velocity) -- Projectile flies from Origin with a Velocity vector
	local StartTime = tick()
	local StartPosition = Origin
	while true do
		if tick() - StartTime >= 3 then print("Unable to find object") break end
		local Delta = RunService.RenderStepped:Wait()
		local ray = Ray.new(StartPosition, Velocity)
		local Hit, HitPosition = workspace:FindPartOnRayWithIgnoreList(ray,{LocalPlayer.Character})
		if Hit then
			print(HitPosition)
			break
		else
			Velocity = Velocity - Vector3.new(0, workspace.Gravity * Delta / 100,0) -- Divide by 100 so projectile doesn't fall too fast (for testing purposes)
			StartPosition = HitPosition
			
			local part = Instance.new("Part")
			part.Size = Vector3.new(1,1,1)
			part.Anchored = true
			part.CanCollide = false
			part.Position = StartPosition
			part.Parent = workspace
		end
	end
end

local Root = LocalPlayer.Character.PrimaryPart
Mouse.Button1Down:Connect(function()
	CalculateLandingPoint(Root.Position, Root.CFrame.LookVector)
end)
1 Like

Thank you so much for the help, it really means a lot that you take the time to help others. Much appreciated! :slight_smile: