Why am I running into this error?

Game Link: (12) Bit Box - Roblox

Hello, I’m making a game similar to the likes of WorldBox, and I’m trying to develop my own system where NPCs can roam and attack other NPCs, but I’m having problems.

The two issues are, Health is not being taken away from the NPCs that get attacked, and the NPC that is attacking never keeps attacking the NPC that they were attacking.

No errors, here is the script.

--\\ Module Made by Star \\--

--[[ --
MoveX:(
Part2 References The Second Part that Part1 is supposed to go to.
)

taskedLocked = Boolean that tells us if their is movement currently going on.

time = time / speed
]] --
local module = {}

local Rep = game:GetService("ReplicatedStorage")
local Effects = Rep:WaitForChild("Effects")

function module:MoveTo(Part, Part2, speed)
	local currentPosition = Part.Position
	local designatedPosition = Part2.Position

	local cPositionX = Part.Position.X
	local cPositionZ = Part.Position.Z

	local dPositionX = designatedPosition.X
	local dPositionZ = designatedPosition.Z

	while currentPosition ~= designatedPosition do
		if (Part2.Position -  Part.Position).Magnitude == 2 then  return end

		task.wait(.25 / speed)

		if cPositionX < dPositionX then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionX > dPositionX then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		if cPositionZ < dPositionZ then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

		dPositionX = designatedPosition.X
		dPositionZ = designatedPosition.Z
	end
end



function module:AttackPerson(Part, Part2, speed) -- Attack Function
	local currentPosition = Part.Position
	local designatedPosition = Part2.Position

	local cPositionX = Part.Position.X
	local cPositionZ = Part.Position.Z

	local dPositionX = designatedPosition.X
	local dPositionZ = designatedPosition.Z

	while currentPosition ~= designatedPosition do
		if (Part2.Position -  Part.Position).Magnitude == 2 then
			local DmgHighlight = Effects:WaitForChild("Highlights"):WaitForChild("Damage"):Clone() DmgHighlight.Parent = Part2 DmgHighlight.Adornee = Part2
			
			local Health = Part2:GetAttribute("Health")
			local Weapon = Part:GetAttribute("Weapon") if Weapon == "Fist" then
				Part2.Position -= Vector3.new(math.random(-8, 8), 0, math.random(-8, 8))
				Health -= math.random(2,5) -- Taking away the health does not work.
				for i = 1,8 do
					task.wait(0.01)
					task.spawn(function()
						DmgHighlight.FillTransparency += .1
					end)
				end
				DmgHighlight:Destroy()
			end
		end -- They don't keep attacking.

		task.wait(.25 / speed)

		if cPositionX < dPositionX then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionX > dPositionX then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		if cPositionZ < dPositionZ then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

		dPositionX = designatedPosition.X
		dPositionZ = designatedPosition.Z
	end
end

return module

2 Likes

Firstly, with your Health issue.

You have to reassign the attribute value once you modify it.

Health -= math.random(2, 5)
Part2:SetAttribute("Health", Health)

Going to need a little longer to figure out the attacking being stopped.

3 Likes

Now your AI issue, you’re checking if the distance of the two parts is EXACTLY 2.
(Part2.Position, Part.Position).Magnitude == 2
Change == to <=

EDIT: Nvm I misread let me re-read

2 Likes

I think the health thing should work perfectly fine, but since I’m using cylinders as NPC’s, won’t they phase through each other?

1 Like

Actually what I initially said was correct.
Within’ your AttackPerson function inside the loop.

if (Part2.Position - Part.Position).Magnitude == 2 then
Change == to <=

You have the similar logic in your MoveTo you don’t have to touch, just AttackPerson.

2 Likes

The Health works now, but this happens when I updated the code.

Whenever the X Value goes up, another value changes in the Part

function module:AttackPerson(Part, Part2, speed)
	local currentPosition = Part.Position
	local designatedPosition = Part2.Position

	local cPositionX = Part.Position.X
	local cPositionZ = Part.Position.Z

	local dPositionX = designatedPosition.X
	local dPositionZ = designatedPosition.Z

	while currentPosition ~= designatedPosition do
		if (Part2.Position -  Part.Position).Magnitude <= 2 then
			local DmgHighlight = Effects:WaitForChild("Highlights"):WaitForChild("Damage"):Clone() DmgHighlight.Parent = Part2 DmgHighlight.Adornee = Part2
			
			local Health = Part2:GetAttribute("Health")
			local Weapon = Part:GetAttribute("Weapon") if Weapon == "Fist" then
				Part2.Position -= Vector3.new(math.random(-8, 8), 0, math.random(-8, 8))
				Health -= math.random(2,4)
				Part2:SetAttribute("Health", Health)
				for i = 1,8 do
					task.wait(0.01)
					task.spawn(function()
						DmgHighlight.FillTransparency += .1
					end)
				end
				DmgHighlight:Destroy()
			end
		end

		task.wait(.25 / speed)

		if cPositionX < dPositionX then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionX > dPositionX then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		if cPositionZ < dPositionZ then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(.5, 0, 0)
		end

		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

		dPositionX = designatedPosition.X
		dPositionZ = designatedPosition.Z
	end
end