Hello Scriptinghelpers community. I made a lead shot turret for a game and for some reason the missile wont hit my Character https://www.youtube.com/watch?v=5NnbdVn9brk it doesnt work with players but it works with parts https://www.youtube.com/watch?v=eKvuiZN2wy8 Here is the script i made:

local turret = script.Parent

local fireRate = 1
local damage = 25
local bulletSpeed = 64
function LeadShotMissile(dist,targetVelocity,MissileSpeed)
	local a = targetVelocity:Dot(targetVelocity) - MissileSpeed*MissileSpeed
	local b = 2*targetVelocity:Dot(dist)
	local c = dist:Dot(dist)
	local det = b*b - 4*a*c
	if(det > 0)then 
		return 2*c/(math.sqrt(det) - b)
	else
		return -1
	end

end
function findTarget()
	local target = nil
	for	i,v in pairs(game.Workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local HRP = v:FindFirstChild("HumanoidRootPart")
		if hum and HRP and hum.Health > 0 then
			target = HRP
		end
	end
	return target
end
while wait(fireRate) do
	local target = findTarget()
	if target then
		local timeToTarget = LeadShotMissile(target.Position - turret.Position,target.Velocity,bulletSpeed)
		turret.CFrame = CFrame.new(turret.Position,target.position + target.Velocity*timeToTarget)
		local flash = Instance.new("PointLight",turret)
			flash.Brightness = 3
			game:GetService("Debris"):AddItem(flash,0.1)
		local bullet = Instance.new("Part")
			bullet.Shape = "Ball"
			bullet.Size = Vector3.new(1,1,1)
			bullet.BrickColor = BrickColor.new("Fossil")
			bullet.Material = Enum.Material.Neon
			bullet.CFrame = turret.CFrame
			bullet.CanCollide = false
			bullet.Touched:Connect(function(obj)
				if not obj:IsDescendantOf(script.Parent.Parent) then 
					local hum = obj.Parent:FindFirstChild("Humanoid")
					if hum then
						
						hum:TakeDamage(damage)
						
					end
					bullet:Destroy()
				end
			end)
			bullet.Parent = workspace

			local bodyVelocity = Instance.new("BodyVelocity",bullet)
			bodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bodyVelocity.Velocity = (turret.CFrame.LookVector) * bulletSpeed
			
			local s = Instance.new("Sound",bullet)
			s.Volume = 0.75
			s.PlaybackSpeed = 7
			s.Looped = true
			s.SoundId = "rbxasset://sounds/Rocket whoosh 01.wav"
			s:Play()
			
			local a1 = Instance.new("Attachment",bullet)
			a1.Position = Vector3.new(0,0.05,0)
			local a2 = Instance.new("Attachment",bullet)
			a2.Position = Vector3.new(0,-0.05,0)
			
			local t = Instance.new("Trail",bullet)
			t.Attachment0 = a1
			t.Attachment1 = a2
			t.Color = ColorSequence.new(bullet.Color)
			t.WidthScale = NumberSequence.new(0.1,0.01)
			t.Lifetime = 0.3
			
			
			game:GetService("Debris"):AddItem(bullet, 5)
		
	end
end

I put the script inside a part, The part is inside a model and the model is inside the workspace. If somebody can help me and tell me where am wrong i would appriciate it.

2 comments

0

Maybe it's because you keep moving

0

Thats the point of a leadshot missile. The turret needs to predict where the target will be based at the time of the hit.