#Introduction

I was creating some NPC's and I tested it. It works fine and all, but when I kill the NPC, it respawns and just stands there, doing nothing. I checked for any errors, and nothing showed up. I searched it up on the internet, still nothing.

#Scripts

Here's my ServerScript inside of the NPC model:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
player.CharacterAdded:Connect(function(character)
	while not character do
		character.AncestryChanged:Wait()
	end
	
	local hrp = character:WaitForChild('HumanoidRootPart')
	local hum = character:WaitForChild('Humanoid')
	local enemyHum = script.Parent.EnemyHum
	local enemyHRP = script.Parent.HumanoidRootPart
	
	local xOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.X
	local yOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.Y
	local zOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.Z
	
	local enemyHRPcf = enemyHRP.Position
	local dead = false
	local inRange = false
	hum.Died:Connect(function()
		dead = true
	end)
	while wait() do
		if (enemyHRP.Position - hrp.Position).Magnitude < 30 then
			if hum.Health > 0 then
				inRange = true
				script.Parent.Pistol.Handle.fire:Play()
				enemyHRP.CFrame = CFrame.new(enemyHRP.Position, hrp.Position)
				enemyHRP.Parent.RightUpperArm.RightShoulder.C0 = CFrame.new(xOffset,yOffset,zOffset) * CFrame.Angles(math.rad(hrp.Orientation.X + 45),0.2,0) * CFrame.Angles(math.rad(hrp.Orientation.X + 45),0.2,0)
				local bulletPart = workspace.bullet:Clone()
				bulletPart.Parent = workspace
				bulletPart.Position = script.Parent.Pistol.Handle.Position
				bulletPart.Transparency = 0
				bulletPart.Anchored = true
				bulletPart.CanCollide = false
				bulletPart.Orientation = (script.Parent.Pistol.Handle.Orientation - hrp.Position) + Vector3.new(90,270,0)
				for i = 0,25,0.5 do
					bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
					wait()
					bulletPart.Touched:Connect(function(hit)
						local t = true
						if hit.Parent:WaitForChild('Humanoid') then
							if t == true then
								t = false
								hum:TakeDamage(50)
								bulletPart:remove()
							end
						end
					end)
				end
				bulletPart:remove()
			elseif (enemyHRP.Position - hrp.Position).Magnitude > 30 then
				inRange = false
			end
		end
	end
end)

#Conclusion

I'm very sorry if the script is hard to read, but I really need an answer. Please help!

3 comments

0

Try adding a while wait(.5) in the beginning and put a break at the end?

0

I dont really work alot with NPCs so sorry if I am not able to help

0

@AntoninFearless Ah, I understand. I'll try it out anyways.