I am making a raycasting turret but it won't work although the script has no errors The turret is supposed to shoot the bullet in ReplicatedStorage It should also turn to face the enemies too The turret doesn't move
local ReplicatedStorage = game:GetService("ReplicatedStorage") local bullet = ReplicatedStorage:WaitForChild("Bullet") local turret = script.Parent local fireRate = 0.5 local bulletDamage = 25 local bulletSpeed = 150 local aggroDist = 100 while wait(fireRate) do end --Find the target, 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 end end 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 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.Postion = turret.Position newBullet.Parent = game.Workspace newBullet.Velocity = turret.CFrame.LookVector * bulletSpeed newBullet.Touched:Connect(function(objectHit) local human = objectHit.Parent:FindFirstChild("Humanoid") if human then human:TakeDamage(bulletDamage) end end) end
You can't just set the object's velocity by manipulating the object's Velocity properties Instead you must use a BodyVelocity for it to apply velocity to the object
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.Postion = turret.Position newBullet.Parent = game.Workspace --newBullet.Velocity = turret.CFrame.LookVector * bulletSpeed local BV = Instance.new("BodyVelocity") --Defining the BodyVelocity BV.Parent = newbullet BV.Velocity = turret.CFrame.LookVector * bulletSpeed game:GetService("Debris"):AddItem(BV,0.3) --Removes the BodyVelocity after 0.3 seconds newBullet.Touched:Connect(function(objectHit) local human = objectHit.Parent:FindFirstChild("Humanoid") if human then human:TakeDamage(bulletDamage) end end) end