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
.