Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script has no errors but the turret won't work?

Asked by 4 years ago
Edited 4 years ago

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

0
It doesn't shoot, or it doesn't even move? Please explain more. Xapelize 2658 — 4y
0
The turret shoots but it doesn't move SUPERMEGADANTHEMAN 33 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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
Ad

Answer this question