Bullet going in wrong direction?

Hello,

I am trying to make an automatic gun that an NPC shoots, it seems to work but the raycast is going in the wrong direction.

Here’s the script:

local shootPart = script.Parent

while wait(math.random(4,8)) do
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.IgnoreWater = true
	local rayCastResults = workspace:Raycast(shootPart.Position,shootPart.Position + Vector3.new(0,0,100))
	local newPart = Instance.new("Part",workspace)
	newPart.Size = shootPart.Size
	local TS = game:GetService("TweenService")
	local TI = TweenInfo.new(1)
	local goals = {["Position"] = (rayCastResults.Position)}
	local track = TS:Create(newPart,TI,goals)
	track:Play()
	newPart.Anchored = true
	newPart.Material = Enum.Material.Neon
	newPart.BrickColor = BrickColor.Yellow()
	game:GetService("Debris"):AddItem(newPart,5)
	newPart.Touched:Connect(function(hit)
		local humanoid = hit.Parent:WaitForChild("Humanoid")
		if humanoid then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr then
				humanoid:TakeDamage(10)
			end
		end
	end)
end

Here’s a video of it:

Thank you for reading this.

The second parameter for raycast is the direction. You can use the LookVector instead of the position + a vector3 value. Make sure the LookVector for the shootPart is the right way though.

Fixed code:

--//Services
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")

--//Variables
local shootPart = script.Parent
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true

--//Controls
local tweenInfo = TweenInfo.new(1)

--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do
	local rayCastResults = workspace:Raycast(shootPart.Position, shootPart.CFrame.LookVector * 100)
	
	local newPart = Instance.new("Part")
	newPart.Size = shootPart.Size
	newPart.Anchored = true
	newPart.Material = Enum.Material.Neon
	newPart.BrickColor = BrickColor.Yellow()
	newPart.Parent = workspace
			
	local track = TS:Create(newPart, tweenInfo, {
		Position = rayCastResults.Position
	})
	
	track:Play()
	
	task.delay(5, newPart.Destroy, newPart)
	
	newPart.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(10)
		end
	end)
end

It errors at line 26: Workspace.ShootPart.Shoot:26: attempt to index nil with 'Position’

Sorry, I made an error.

Try this:

--//Services
local Players = game:GetService("Players")

--//Variables
local shootPart = script.Parent

--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do
	local rayCastResults = workspace:Raycast(shootPart.Position, shootPart.CFrame.LookVector * 100)
	
	local newPart = Instance.new("Part")
	newPart.Size = shootPart.Size
	newPart.Anchored = false
	newPart.Material = Enum.Material.Neon
	newPart.BrickColor = BrickColor.Yellow()
	newPart.Parent = workspace
	
	newPart:ApplyImpulse(shootPart.CFrame.LookVector * 1000)
	
	task.delay(5, newPart.Destroy, newPart)
	
	newPart.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(10)
		end
	end)
end

The bullet doesn’t appear

ingore this limit bypass :slightly_smiling_face:

Could be because I made the bullet speed too fast.

Try this:

--//Services
local Players = game:GetService("Players")

--//Variables
local shootPart = script.Parent

--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do	
	local newPart = Instance.new("Part")
	newPart.Size = shootPart.Size
	newPart.Position = shootPart.Position + shootPart.CFrame.LookVector * 5
	newPart.Anchored = false
	newPart.Material = Enum.Material.Neon
	newPart.BrickColor = BrickColor.Yellow()
	newPart.Parent = workspace
	
	newPart:ApplyImpulse(shootPart.CFrame.LookVector * 1000)
	
	task.delay(5, newPart.Destroy, newPart)
	
	newPart.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(10)
		end
	end)
end

Still doesn’t appear, maybe you made :ApplyImpulse too fast.

Are you sure you made the shootpart appear the right way? Also, if you could send me the model in a .rbxm so I can test it by myself and then send it back, it would be appreciated.

How do I make the model a .rbxm file?

Right click the model, and press save to file.

Ok, here’s the model
dummytower.rbxm (45.6 KB)

Nevermind, I just slowed it down a bit by lowering the lookVector value.

Fixed file:
dummytower.rbxm (45.8 KB)

This one uses TweenService to visualize the bullet and a raycast to damage the player.
The other one uses ApplyImpulse to visualize the bullet and .Touched to damage the player.

It’s your choice which one you choose.

Alright, thank you so much.

limit :slightly_smiling_face: :slightly_smiling_face: :slightly_smiling_face:

No problem. If you have any more questions, feel free to ask.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.