Why is my turret system not detecting players?

So right now i have a turret system thats supposed to shoot any player near and not shoot any player behind a wall.

when a player moves behind a wall or when there already is a wall infront of it when the player reappears the turret dosent shoot it anymore.

this is the current code and its in a server script

local turret = script.Parent
local IgnoreParts = {turret}
local bulletdamage = 15
local bulletspeed = 300
local aggrodist = 50
local firerate = .1
local bullet = game.ReplicatedStorage:WaitForChild("Bullet")
local NewBullet
local update = function()
    local isntance = game.Players:GetChildren()
    for i, v in pairs(isntance) do
        print(typeof(v))
        table.insert(IgnoreParts, v.Character)
    end
end
while true do
    wait()
    update()
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = IgnoreParts
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local Wall, HitPosition, Normal, Materia = workspace:Raycast(turret.Position, turret.CFrame.LookVector*aggrodist, raycastParams)
    if Wall then
    else
        print("B)")
        wait(firerate)
        local root = nil
        for i,v in pairs(game.Workspace:GetChildren()) do
            if v.Name == script.Parent.Parent.Owner.Value then
                target = nil
            else
                local human = v:FindFirstChild("Humanoid")
                root = v:FindFirstChild("HumanoidRootPart")
                if human and root and human.Health > 0 then
                    if (root.Position - turret.Position).magnitude < aggrodist then
                    else
                        root = nil
                    end
                end
            end
        end
        if root then
            turret.CFrame = CFrame.new(turret.Position, root.Position)
            NewBullet = bullet:Clone()
            NewBullet.Parent = workspace
            NewBullet.Position = turret.Position
            NewBullet.Velocity = turret.CFrame.LookVector * bulletspeed
        end
        root = nil
    end
end

Can someone help me?

4 Likes

Not necessarily a solution, but.

Since nobody responded… you could try printing the “root” variable and maybe also the “target” variable.

Keep printing them, and see what they print when it bugs out and doesn’t shoot the player anymore.

ok t hanks for the suggestion ill try it out

So I did some testing with your code and I think I realized what might have been the problem. The way you have it set up, It seems like the turrent will only rotate when the raycast doesn’t hit anything. The problem with this is that if your turrent ever faces a wall, it will never be able to rotate again because the raycast will continue to hit that wall. So if the turrent is shooting at a player and they run behind a wall, the turrent will continue facing the wall and the rest of your code will never be able to run again.

I think this should fix it:

local turret = script.Parent
local IgnoreParts = {turret}
local bulletdamage = 15
local bulletspeed = 300
local aggrodist = 50
local firerate = .1
local bullet = game.ReplicatedStorage:WaitForChild("Bullet")
local NewBullet

local update = function()
	local isntance = game.Players:GetChildren()
	for i, v in pairs(isntance) do
		table.insert(IgnoreParts, v.Character)
	end
end

while true do
	wait()
	update()
	
	local target -- this will tell us which rootpart to shoot at  
		
	for i,v in pairs(game.Workspace:GetChildren()) do
		if v.Name ~= script.Parent.Parent.Owner.Value then -- I changed this up a little bit, it should do the same thing you want though 			
			local human = v:FindFirstChild("Humanoid")
			root = v:FindFirstChild("HumanoidRootPart")
			if human and root and human.Health > 0 then
				local raycastParams = RaycastParams.new() -- here is where you want to raycast, to see if anything is blocking the Turrent from this rootpart
				raycastParams.FilterDescendantsInstances = IgnoreParts
				raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
				local direction = (root.Position - turret.Position) -- this is just the direction from the turrent to the players rootpart 
				local Wall, HitPosition, Normal, Materia = workspace:Raycast(turret.Position, direction, raycastParams)	 -- raycast to see if there is a part in front of the player 
				if (root.Position - turret.Position).magnitude < aggrodist and not Wall then -- check to see if player is within range and nothing is blocking them
					target = root 
				end
			end
		end
	end
	
	if target then 
		turret.CFrame = CFrame.new(turret.Position, target.Position)
		NewBullet = bullet:Clone()
		NewBullet.CFrame = turret.CFrame
		NewBullet.Parent = workspace
		NewBullet.Anchored = false 
		NewBullet.Velocity = turret.CFrame.LookVector * bulletspeed
		wait(firerate)
	end	
	target = nil
end

I tried not to change up your code too much, hope this helps!

Works like a charm :grinning: thanks for the help!

1 Like