Turret won't fire

Hey developers I was wondering if any of you could help me with my script?
I am currently making a turret that fires bullets and my script Isn’t working. MY BULLET IS A MODEL

local bullet = game:GetService("ReplicatedStorage").Bullet
local turret = script.Parent.BARREL
local bulletdmg = 5
local bulletspeed = 250
local agrodist = 300
local firerate = 0.5
while wait(firerate) do 
	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 < agrodist then
				target = torso
				end
			end
		
	end
	if target then
		print(target.Name)
		local torso = target	
		turret.CFrame = CFrame.new(turret.CFrame,torso.CFrame)
		local newbullet = bullet:Clone()
		newbullet.Position = turret.Position
		newbullet.Parent = game.Workspace
		newbullet.Velocity = turret.CFrame.LookVector * bulletspeed		
	end
end

Models don’t have a Position property - try changing the position of the bullet part inside the model instead.

I changed up my code a little now I have

local bullet = game:GetService("ReplicatedStorage").Bullet
local turret = script.Parent.BARREL
local bulletdmg = 5
local bulletspeed = 250
local agrodist = 300
local firerate = 0.5
while wait(firerate) do 
   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 < agrodist then
   			target = torso
   			end
   		end
   	
   end
   if target then
   	print(target.Name)
   	local torso = target	
   	turret.CFrame = CFrame.new(turret.CFrame,torso.CFrame)
   	local newbullet = bullet:Clone()
   	newbullet.main.Position = turret.Position
   	newbullet.Parent = game.Workspace
   	newbullet.BodyVelocity.Velocity = turret.CFrame.LookVector * bulletspeed		
   end
end

and I still have no errors however the turret will not even rotate towords me.

Only thing I can think of is if Torso doesn’t exist, which it wouldn’t if your character is an R15 rig. I check for HumanoidRootPart instead, as that’s present in any character rig, is more or less in the same location, and is also essential.

You can use primary CFrame but I understand that won’t be ideal.

Also I think it would only be for _, v in pairs(blah blah blah here) do but I can’t be for sure.

Anyhow I hope this helps.

SetPrimaryPartCFrame is useful, however it carries a few drawbacks: you can’t use TweenService with it, and each use incurs a floating point penalty, making all the parts in the model shift slightly.

1 Like

Thanks everyone it is all solved now.

1 Like