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

Need help with auto turret i made to test out raycasting, how should i fix this?

Asked by 3 years ago

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
0
i think its a problem with the for i,v in pairs(game.Workspace:GetChildren()) do since after that line you have one that waits for a humanoid. what if V is some random part in the workspace? the script would stop there untill you add a humanoid into that. marsdonh 36 — 3y
0
to test this can you put a print(v.Name) right after the for i,v in pairs(game.Workspace:GetChildren()) do line. then tell me what is prints. marsdonh 36 — 3y
0
It printed 'Camera' so I should probably do something like if v.Name ~= "Camera" or something like that. munkuush 22 — 3y
0
Ok so it didnt print camera this time but it printed Baseplate so i should find a way to use something like :IsA() or something like that munkuush 22 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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.

Ad
Log in to vote
0
Answered by 3 years ago

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

Answer this question