✓ Accepted answer
Kinda sounds like your targeting code is getting confused with things in the workspace.
try this code:
local RS = game:GetService("ReplicatedStorage")
local bullet = RS:WaitForChild("Bullet")
local Turret = script.Parent
local fireRate = 1
local Damage = 20
local bulletSpeed = 250
local detectDistance = 50
function getHumanoidTarget()
local target = nil
local Dist = detectDistance
for k,v in pairs(workspace:GetChildren()) do
local Humanoid = v:FindFirstChildOfClass("Humanoid")
local Torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
if(Humanoid ~= nil and Humanoid.Health > 0 and Torso ~= nil) then
local dist = (Torso.Position - Turret.Position).magnitude
if(dist < Dist) then
Dist = dist
target = v
end
end
end
return target
end
while wait(fireRate) do
local target = nil
local tgt = getHumanoidTarget()
if(tgt ~= nil) then
local torso = tgt:FindFirstChild("Torso") or tgt:FindFirstChild("HumanoidRootPart")
if(torso ~= nil) then
if (torso.Position - Turret.Position).magnitude <= detectDistance then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {workspace.Turret}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local BulletPathResult = workspace:Raycast(Turret.Position,(torso.Position - Turret.Position).Unit*detectDistance, raycastParams)
if BulletPathResult.Instance == torso
or BulletPathResult.Instance.Parent:FindFirstChild(torso.Name) then
print("Targeting the humanoid")
target = torso
else
target = nil
print("Object is in the way")
end
end
end
if target then
local torso = target
Turret.CFrame = CFrame.new(Turret.Position, torso.Position)
print("Fire")
local bulletClone = bullet:Clone()
bulletClone.Parent = game.Workspace
bulletClone.Position = Turret.Position
bulletClone.Velocity = Turret.CFrame.LookVector * bulletSpeed
bulletClone.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(Damage)
bulletClone:Destroy()
end
wait(3)
bulletClone:Destroy()
end)
end
end
end
a remix of your turret code to follow the target humanoid when it "sees it" :)
Integrated FPS code used for the Firing sequence, Since this code is running every game tick!.
Make the turret Follow you and fire code:
local RS = game:GetService("ReplicatedStorage")
local bullet = RS:WaitForChild("Bullet")
local Turret = script.Parent
local fireRate = 1
local Damage = 20
local bulletSpeed = 250
local detectDistance = 50
function getHumanoidTarget()
local target = nil
local Dist = detectDistance
for k,v in pairs(workspace:GetChildren()) do
local Humanoid = v:FindFirstChildOfClass("Humanoid")
local Torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
if(Humanoid ~= nil and Humanoid.Health > 0 and Torso ~= nil) then
local dist = (Torso.Position - Turret.Position).magnitude
if(dist < Dist) then
Dist = dist
target = v
end
end
end
return target
end
local t = 0
local FPS = 0
local time = tick()
while true do
local NowTime = tick()
local dt = NowTime - time
time = tick()
local FPS = (1/dt)
local target = nil
local tgt = getHumanoidTarget()
if(tgt ~= nil) then
local torso = tgt:FindFirstChild("Torso") or tgt:FindFirstChild("HumanoidRootPart")
if(torso ~= nil) then
if (torso.Position - Turret.Position).magnitude <= detectDistance then
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {workspace.Turret}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local BulletPathResult = workspace:Raycast(Turret.Position,(torso.Position - Turret.Position).Unit*detectDistance, raycastParams)
if BulletPathResult.Instance == torso
or BulletPathResult.Instance.Parent:FindFirstChild(torso.Name) then
print("Targeting the humanoid")
target = torso
else
target = nil
print("Object is in the way")
end
end
end
if target then
local torso = target
Turret.CFrame = CFrame.new(Turret.Position, torso.Position)
if(t%math.floor((fireRate*FPS) +0.5) == 0) then
print("Fire")
local bulletClone = bullet:Clone()
bulletClone.Parent = game.Workspace
bulletClone.Position = Turret.Position
bulletClone.Velocity = Turret.CFrame.LookVector * bulletSpeed
bulletClone.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(Damage)
bulletClone:Destroy()
end
wait(3)
bulletClone:Destroy()
end)
end
end
end
wait()
t = t +1
end
2 comments
Thanks a lot! I'll implement this later to my turret, I just have one question, that is do I have to use both scripts or I just choose the system I want to use? (I want to use the first one just because it's easier to understand for me and I haven't leaned about stuff like ticks and stuff like that yet.)
either one will work., the 2nd one works on game tick. though its a little buggy when it comes to changing the firerate (it tends to be a machinegun or not fire sometimes), since i wrote it in 5 mins lol :P