Issue with cosmetic bullets

I have an NPC that does damage to other NPCs every certain amount of time. In the shoot script, I create a purely cosmetic part to act as the bullet. The problem is that it teleports the NPC for some reason.

Here is my code: (ServerScript)

local currentSoldier = script.Parent
local canShoot = script.Parent.CanShoot
local target = script.Parent.Target
local damage = 10
local reload = 1 --In Seconds

local function _inRange()
	while target.Value ~= nil and canShoot.Value ~= false do
		local enemy = target.Value
		
		if enemy.Humanoid.Health <= 0 then
			target.Value = nil
			break
		end
		
		local distance = (currentSoldier.PrimaryPart.Position - enemy.PrimaryPart.Position).magnitude
		
		local bullet = Instance.new("Part", workspace)
		bullet.CanCollide = false
		bullet.Anchored = true
		bullet.Size = Vector3.new(distance, 0.25, 0.25)
		bullet.BrickColor = BrickColor.new("Bright yellow")
		bullet.Name = "Bullet"
		
		local midPointX = (currentSoldier.PrimaryPart.Position.X + enemy.PrimaryPart.Position.X) / 2
		local midPointY = (currentSoldier.PrimaryPart.Position.Y + enemy.PrimaryPart.Position.Y) / 2
		local midPointZ = (currentSoldier.PrimaryPart.Position.Z + enemy.PrimaryPart.Position.Z) / 2
		local midPoint = Vector3.new(midPointX, midPointY, midPointZ)
		
		bullet.Position = midPoint
		currentSoldier.PrimaryPart.CFrame = CFrame.lookAt(bullet.Position, enemy.PrimaryPart.Position)
		enemy.Humanoid:TakeDamage(damage)
		
		task.wait(reload / 2)
		bullet:Destroy()
		task.wait(reload / 2)
	end
end

target.Changed:Connect(_inRange)

canShoot.Changed:Connect(_inRange)

There are no errors, and when I print the positions of the currentSoldier, enemy, and midPoint, they are all the same for some reason.

Edit:

Other Scripts

Range Script:

local runService = game:GetService("RunService")
local soldiersFolder = workspace:WaitForChild("Soldiers")

local currentSoldier = script.Parent
local soldeirsInRange = {}

local closestDistance = math.huge
local closestSoldier = nil

local canLook = currentSoldier.CanLook
local team = currentSoldier.Team
local target = currentSoldier.Target

runService.Heartbeat:Connect(function()
	closestDistance = math.huge

	for i, Soldier in soldiersFolder:GetChildren() do
		if Soldier ~= currentSoldier and Soldier.Team.Value ~= team.Value and Soldier.Humanoid.Health > 0 then
			local distance = (currentSoldier.PrimaryPart.Position - Soldier.PrimaryPart.Position).magnitude

			if distance <= 10 then
				if not table.find(soldeirsInRange, Soldier) then
					table.insert(soldeirsInRange, Soldier)
				end
				
				if distance < closestDistance then
					closestDistance = distance
					closestSoldier = Soldier
					target.Value = closestSoldier

					if canLook.Value == true then
						currentSoldier.PrimaryPart.CFrame = CFrame.lookAt(currentSoldier.PrimaryPart.Position, closestSoldier.PrimaryPart.Position)
					end
				end
			end
		end
	end
end)

Move Script:

local highlight = script.Parent.Highlight
local clickDetector = script.Parent.ClickDetector
local selected = script.Parent.Selected
local playerWhoSelected = script.Parent.PlayerWhoSelected
local team = script.Parent.Team
local canLook = script.Parent.CanLook
local canShoot = script.Parent.CanShoot
local clickEvent = script.Parent.ClickEvent

clickDetector.MouseClick:Connect(function(player)
	if player.Team.Name == team.Value then
		if not selected.Value then
			selected.Value = true
			playerWhoSelected.Value = player
			highlight.Enabled = true
		else
			selected.Value = false
			playerWhoSelected.Value = nil
			highlight.Enabled = false
		end
	end
end)

clickEvent.OnServerEvent:Connect(function(player, hitPos)
	canLook.Value = false
	canShoot.Value = false
	script.Parent.Humanoid:MoveTo(hitPos)
	
	script.Parent.Humanoid.MoveToFinished:Connect(function()
		canLook.Value = true
		canShoot.Value = true
	end)
end)

Both of the above scripts are ServerScripts.

Video

I may be missing something but I do not see any code even trying to change the bullet’s size, Are you sure you did not forget to code something?

Oops, I just realised my problem here, so I just changed it to this:

bullet.Size = Vector3.new(0.25, 0.25, distance)

Thanks lol.

1 Like

You are welcome, If possible please mark my reply as the solution, I am pretty new to the DevForum and I am trying to build up a reputation for myself.

1 Like

The issue isn’t completely fixed as I still have the second issue:

Here is a video:

You are not modifying the position of the soldier in this code, check your other scripts.

Ik, that’s why I’m puzzled. This wasn’t happening before I added this script, and I’ve checked all my scripts for such an issue but couldn’t find one. I’ll post the only other scripts here too:

Range Script:

local runService = game:GetService("RunService")
local soldiersFolder = workspace:WaitForChild("Soldiers")

local currentSoldier = script.Parent
local soldeirsInRange = {}

local closestDistance = math.huge
local closestSoldier = nil

local canLook = currentSoldier.CanLook
local team = currentSoldier.Team
local target = currentSoldier.Target

runService.Heartbeat:Connect(function()
	closestDistance = math.huge

	for i, Soldier in soldiersFolder:GetChildren() do
		if Soldier ~= currentSoldier and Soldier.Team.Value ~= team.Value and Soldier.Humanoid.Health > 0 then
			local distance = (currentSoldier.PrimaryPart.Position - Soldier.PrimaryPart.Position).magnitude

			if distance <= 10 then
				if not table.find(soldeirsInRange, Soldier) then
					table.insert(soldeirsInRange, Soldier)
				end
				
				if distance < closestDistance then
					closestDistance = distance
					closestSoldier = Soldier
					target.Value = closestSoldier

					if canLook.Value == true then
						currentSoldier.PrimaryPart.CFrame = CFrame.lookAt(currentSoldier.PrimaryPart.Position, closestSoldier.PrimaryPart.Position)
					end
				end
			end
		end
	end
end)

Move Script:

local highlight = script.Parent.Highlight
local clickDetector = script.Parent.ClickDetector
local selected = script.Parent.Selected
local playerWhoSelected = script.Parent.PlayerWhoSelected
local team = script.Parent.Team
local canLook = script.Parent.CanLook
local canShoot = script.Parent.CanShoot
local clickEvent = script.Parent.ClickEvent

clickDetector.MouseClick:Connect(function(player)
	if player.Team.Name == team.Value then
		if not selected.Value then
			selected.Value = true
			playerWhoSelected.Value = player
			highlight.Enabled = true
		else
			selected.Value = false
			playerWhoSelected.Value = nil
			highlight.Enabled = false
		end
	end
end)

clickEvent.OnServerEvent:Connect(function(player, hitPos)
	canLook.Value = false
	canShoot.Value = false
	script.Parent.Humanoid:MoveTo(hitPos)
	
	script.Parent.Humanoid.MoveToFinished:Connect(function()
		canLook.Value = true
		canShoot.Value = true
	end)
end)

Both of the above scripts are ServerScripts.

It is a problem with your move script, check the function at the bottom

I deleted the move script and then tested it by manually moving the NPC near the enemy, but the problem still occurs.

1 Like

Did you weld the bullet to the NPC?

1 Like

Nope. I checked for welds inside of the NPCs primary part as well as inside the bullet but found nothing.

Welp, I am puzzled, Sorry that I could not help but you should probably open a new thread as it will get bumped to the top and people will help you with your OG problem.

Same here. Thanks for the help you were able to provide!

I figured out what the problem was…

I forgot to change this line to this:

bullet.CFrame = CFrame.lookAt(bullet.Position, enemy.PrimaryPart.Position)

Oops…

Thanks for your help! I’ll mark your first post as the solution as it solved one of the problems which both turned out to be me forgetting something lol. Usually seams to be along those lines.

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