#Can someone help me with my raycasting script its not killing the player

6 messages · Page 1 of 1 (latest)

formal hemlock
#

This is the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bullet = ReplicatedStorage:WaitForChild("Bullet")

local turret = script.Parent

local fireRate = 0.5
local bulletDamage = 10
local bulletSpeed = 150
local agroDist = 100

while wait(fireRate) do

--Find the target, detect if its realistic to shoot
local target = nil
for i, v in pairs(workspace:GetChildren()) do
    local human = v:FindFirstChild("Humanoid")
    local hr = v:FindFirstChild("HumanoidRootPart")
    if human and hr and human.Health > 0 then
        if (hr.Position - turret.Position).magnitude < agroDist then
            target = hr
            
        end
    end
end
if target then
    local hr = target
    --Turn the turret to face the target
    turret.CFrame = CFrame.new(turret.Position, hr.Position)
    
    local newBullet =  Bullet:Clone()
    newBullet.Position = turret.Position
    newBullet.Parent = workspace
    
    newBullet.Velocity = turret.CFrame.LookVector * bulletSpeed
    
    newBullet.Touched:Connect(function(hit)
        local human = hit:FindFirstChild("Humanoid")
        if human.Health > 0 then
        
            human:TakeDamage(10)
        end
    end)
end

end

#

also if anyone has like a youtube video or Roblox Studio web link that fixs the problem(besides the raycasting documentation) then please drop them below

hollow elm
formal hemlock
raw talon
#

u should check if the hit is a human before you define it

formal hemlock
#

so I found out the problem to the script and basically it was because I didnt check the health lol