Not spawning bullet from turret

What’s going on here?

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

local turret = script.Parent

local fireRate = .5
local bullet_damage = 10
local bulletSpeed = 150
local aggrodist = 100

while wait(fireRate) do
	
	--find the target and 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
			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
		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.Position = turret.Position
		newbullet.Parent = workspace
		
		newbullet.Velocity = turret.CFrame.LookVector * bulletSpeed
		
		newbullet.Touched:Connect(function(objecthit)
			local human = objecthit.Parent:FindFirstChild("Humanoid")
			if human then
				human:TakeDamage(bullet_damage)--take damage
			end
		end)
	end
end

The bullet isn’t spawning for some reason, and I get one obnoxious output error:
builtin_SelectDragger.rbxm.SelectDragger.Packages.DraggerFramework.Utility.ViewChangeDetector:14: attempt to index nil with 'CFrame'

Not even sure what this output means, help please.

Is the bullet in ReplicatedStorage?

1 Like

Yes it is. charrrrrrrrrrrrrrrrrr

I think it’s having trouble with this part of the script? image

1 Like

I don’t know how to script or anything but I think the error is coming from that.

1 Like

Perhaps check if objecthit or objecthit.Parent is nil.

newbullet.Touched:Connect(function(objecthit)
    if objecthit and objecthit.Parent then -- here
        local human = objecthit.Parent:FindFirstChild("Humanoid")
	    if human then
		    human:TakeDamage(bullet_damage)
	    end
    end
end)

Edit: wrong person replied to, sorry

cc @Infuriashon

1 Like

Most likely it is because your script.Parent is a model, do turret.PrimaryPart.CFrame. But to add on you are inversing your ray here

so like it will shoot in the opposite direction of the player.

script.Parent is the barrel of the gun not the model

It stopped the output from firing, but the bullets still aren’t spawning.

Maybe the memory state has been changed (from a server handler perhaps?), try doing game.ReplicateStorage.Bullet:Clone instead of localizing it, or at least redefine it before.

1 Like

Well I need to add stuff to the cloned bullet so I need to localize it, right?

What specifically is “Who”, and what relation does it have to the server?

I’m confused on what you’re saying here. Where are you getting this from?

Sorry was helping somebody else and your code structures were really similar lol

1 Like

Oh I found the bugger, you have a uninitialized variable (target) basically in C++ we would do if (&target) but there is no pointers in lua so idk what to tell you how to fix it.

EDIT: make “target” a global (remove the local)

I put target at the top of the script so it’s localized everywhere(in the script).

ohh, hmm. Maybe it cause u only call the if statement once at run time (read your end statements very carefully)

1 Like

If you like, if you save the place to file and DM me it, I can take a look at it for you. I don’t really want to clog up the thread with lots of small suggestions, but I’ll let you know if I get it working.

1 Like