#Ray.new always prints something on the way, even when it is nothing. Making a turret

18 messages · Page 1 of 1 (latest)

brave horizon
#

Hi! I'm trying to make a turret that aims bullets at player and damages them, everything working fine, except when I add the Ray.new to see if something is on the way, like a wall, it is always gives me the msg that something on the way. Thanks!
``​lua
local fireWhenPlayer = script.Parent

local replicatedStorage = game:GetService("ReplicatedStorage")
local bullet = replicatedStorage.Bullet
local turretBase = game.Workspace.TurretBase
local turretFire = game.Workspace.TurretFire

local fireRate = 0.3
local bulletSpeed = 100
local bulletDamage = 10
local agroDistnace = 50

local bulletFolder = Instance.new("Folder")
bulletFolder.Parent = script.Parent
bulletFolder.Name = "BulletFolder"

while wait(fireRate) do
local target
for i, v in pairs(game.Workspace:GetChildren()) do

    local hum = v:FindFirstChild("Humanoid")
    local upperTorso = v:FindFirstChild("UpperTorso")
    
    if hum and upperTorso then
        if (upperTorso.Position - turretFire.Position).Magnitude < agroDistnace then
            local newRay = Ray.new(turretFire.Position, (upperTorso.Position - turretFire.Position).Unit * agroDistnace)
            local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(newRay, {turretFire, turretBase})
            if hit == upperTorso then
                target = upperTorso
            else
                print("Something on the way")
            end
        end
    end
end

if target then

...
end
``

pastel ether
#

use workspace:Raycast

brave horizon
#

Thanks, I tried to change it but now I'm stuck with the problem that bullets go through walls, I know I'm doing something wrong but I just don't understant and can't find a solution anywhere

``​lua
while wait(fireRate) do
local target
for i, v in pairs(game.Workspace:GetChildren()) do

    local hum = v:FindFirstChild("Humanoid")
    local upperTorso = v:FindFirstChild("UpperTorso")
    
    if hum and upperTorso then
        if (upperTorso.Position - turretFire.Position).Magnitude < agroDistnace then
            
            local params = RaycastParams.new()
            params.FilterDescendantsInstances = {script.Parent}
            params.FilterType = Enum.RaycastFilterType.Exclude
            
            local raycastResult = workspace:Raycast(turretFire.Position, (upperTorso.Position - turretFire.Position).Unit * agroDistnace, params)
            
            if raycastResult then
                target = upperTorso
            else
                print("Something on the way")
            end
        end
    end
end

``​

pastel ether
#

ur not doing anything to stop the bullet

brave horizon
#

hm i see.. i’m gonna try to change something tomorrow maybe it will work

pastel ether
#

what are u using

#

to

#

move the bullet

#

btw

#

u could just destroy the bullet

#

after it hits something

brave horizon
# pastel ether move the bullet

I use the velocity, it is in ''if target then..'' function, I actually destroy bullets after they hit the target, but I don't want it to shoot to the wall while behind it a player, I don't want it to see the character if something on the way of the ray, or my turret gonna have a wallhack xd

solar iceBOT
#

studio** You are now Level 1! **studio

brave horizon
#

? what do you mean

pastel ether
#

I use the velocity, it is in ''if target then..'' function

#

you should destroy it

#

there