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
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.
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.