So i wanted to make something and I have also followed a tutorial however it is not working and everything i do to it wont fix it and another thing is the while wait() do loop is only playing once and i am also not getting any errors in output.
local turret = script.Parent local bulletdamage = 15 local bulletspeed = 150 local aggrodist = 75 local firerate = .3 local bullet = game.ReplicatedStorage:WaitForChild("Bullet") while wait(firerate) do print("Checking") local target = nil for i,v in pairs(game.Workspace:GetChildren()) do local human = v:WaitForChild("Humanoid") local root = v:WaitForChild("HumanoidRootPart") if human and root and human.Health > 0 then if (root.Position - turret.Position).magnitude < aggrodist then target = root end end end if target then local root = target turret.CFrame = CFrame.new(turret.Position, root.Position) local NewBullet = bullet:Clone() NewBullet.Parent = workspace NewBullet.Position = turret.Position NewBullet.Velocity = turret.CFrame.LookVector * bulletspeed NewBullet.Touched:Connect(function(hit) local hum = hit.Parent:WaitForChild("Humanoid") if hum then hum:TakeDamage(bulletdamage) NewBullet:Destroy() elseif hit ~= turret then NewBullet:Destroy() end end) end end
Just realised that instead of doing 'WaitForChild' i could do 'FindFirstChild', so it wont have to wait for it and instead it will just search for it while also allowing it to run other things in workspace to search for.
so the problem with this is that the for loop will run through everything in the workspace. but the right after it
local human = v:WaitForChild("Humanoid")
is waiting for a instance called Humanoid but v could be some random part in the workspace. for this example v = Camera. but there is no instance called Humanoid under camera. so the loop would just stop here until a Humanoid instance pops up. to fix this you can make it so that if there is no humanoid under the instance then continue the loop and go to the next instance. i cant think of a better way someone else may but this is how i would do it.
local turret = script.Parent local bulletdamage = 15 local bulletspeed = 150 local aggrodist = 75 local firerate = .3 local bullet = game.ReplicatedStorage:WaitForChild("Bullet") local findhumanoid = function(part) for i, v in pairs(part:GetChildren()) do if v.Name == "Humanoid" then return(v) else return(v) end end end local HumanoidRootPart = function(part) for i, v in pairs(part:GetChildren()) do if v.Name == "HumanoidRootPart" then return(true) else return(false) end end end while wait(firerate) do local target = nil for i,v in pairs(game.Workspace:GetChildren()) do local found1 = findhumanoid(v) if found1.Name == "Humanoid" then local human = found1 local found2 = HumanoidRootPart(v) if found2.Name == "HumanoidRootPart" then root = found2 if human and root and human.Health > 0 then if (root.Position - turret.Position).magnitude < aggrodist then target = root end end end end end if target then local root = target turret.CFrame = CFrame.new(turret.Position, root.Position) local NewBullet = bullet:Clone() NewBullet.Parent = workspace NewBullet.Position = turret.Position NewBullet.Velocity = turret.CFrame.LookVector * bulletspeed NewBullet.Touched:Connect(function(hit) local hum = hit.Parent:WaitForChild("Humanoid") if hum then hum:TakeDamage(bulletdamage) NewBullet:Destroy() elseif hit ~= turret then NewBullet:Destroy() end end) end end
if you have any problems comment on this response