local replicated_storage = game:GetService("ReplicatedStorage")
local bullet = replicated_storage:WaitForChild("Bullet")
local turret = script.Parent
local fireRate = .5
local bullet_damage = 10
local bulletSpeed = 150
local aggrodist = 100
while wait(fireRate) do
--find the target and detect if it's realistic to shoot
local target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and human.Health > 0 then
if (torso.Position - turret.Position).Magnitude < aggrodist then
local bulletray = Ray.new(turret.Position, (torso.Position - turret.Position).Unit * 500)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletray, (turret))
if hit == torso then
target = torso
else
print("Object in the way!")
end
end
end
end
if target then
local torso = target
--turn the turret to face the target
turret.CFrame = CFrame.new(turret.Position, torso.Position)
local newbullet = bullet:Clone()
newbullet.Position = turret.Position
newbullet.Parent = workspace
newbullet.Velocity = turret.CFrame.LookVector * bulletSpeed
newbullet.Touched:Connect(function(objecthit)
local human = objecthit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(bullet_damage)--take damage
end
end)
end
end
The bullet isn’t spawning for some reason, and I get one obnoxious output error: builtin_SelectDragger.rbxm.SelectDragger.Packages.DraggerFramework.Utility.ViewChangeDetector:14: attempt to index nil with 'CFrame'
Not even sure what this output means, help please.
Perhaps check if objecthit or objecthit.Parent is nil.
newbullet.Touched:Connect(function(objecthit)
if objecthit and objecthit.Parent then -- here
local human = objecthit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(bullet_damage)
end
end
end)
Maybe the memory state has been changed (from a server handler perhaps?), try doing game.ReplicateStorage.Bullet:Clone instead of localizing it, or at least redefine it before.
Oh I found the bugger, you have a uninitialized variable (target) basically in C++ we would do if (&target) but there is no pointers in lua so idk what to tell you how to fix it.
If you like, if you save the place to file and DM me it, I can take a look at it for you. I don’t really want to clog up the thread with lots of small suggestions, but I’ll let you know if I get it working.