Script keeps timing out


  17:28:12.949  ServerScriptService.CoreScripts.Move:11: Script timeout: exhausted allowed execution time  -  Server - MoveModule:67
  17:28:12.949  Stack Begin  -  Studio
  17:28:12.949  Script 'ServerScriptService.Modules.MoveModule', Line 67 - function AttackPerson  -  Studio - MoveModule:67
  17:28:12.949  Script 'ServerScriptService.CoreScripts.Move', Line 11  -  Studio - Move:11
  17:28:12.949  Stack End  -  Studio
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
	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

return module

1 Like

Can you drop the line that it’s referencing?
The code you gave doesn’t match line numbers in the error.

1 Like
	while currentPosition ~= designatedPosition do
1 Like

Add a wait() since the only wait you have is when the two parts are in range.

As @GetGlobals stated, you should use task.wait() in order to make the script not time out. When you use a while loop without any wait time, the script will time out. If you already have it, make sure that it runs no matter what, even if any conditions are met or not.

2 Likes

Now it just is broken.

Nothing works in the module script

1 Like
while currentPosition ~= designatedPosition do
		task.wait(1)
1 Like

Can I see your code that repeatedly calls the module?

1 Like

It only fire’s once

local Part1 = workspace:WaitForChild("Person1")
local Part2 = workspace:WaitForChild("Person2")

local MoveModule = require(script.Parent.Parent:WaitForChild("Modules"):WaitForChild("MoveModule"))
local ArrayModule = require(script.Parent.Parent:WaitForChild("Modules"):WaitForChild("ArrayModule"))

print("Server Script Ready!")

task.wait(5)

MoveModule:AttackPerson(Part1, Part2, 7.5)
1 Like

It says it is being called at line 11 of CoreScripts.Move. Is this in that area?

1 Like

Its the module scripts that Im showing?

1 Like

1 Like

What about the second line shown here? Can I see that script?

1 Like

Do this:

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()
	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

return module
1 Like

Could you explain what you changed and how it will fix my script?

1 Like
--\\ 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
]] --
1 Like

I added task.wait out of the if statement which means it will run the while loop with a timeout if the if statement doesn’t work

1 Like

??? I told him to do that originally in my first post, but I guess he didn’t do it.

1 Like

There’s an obvious difference to that, they actually told me where to put it.

I showed you where I put it, it broke the module script, and the script stopped working.

1 Like

Let’s say the distance between the two parts is over 2 studs, it will run no code because you didn’t add an else condition.

This is fine because you don’t need one, but if it runs without waiting, it will just be running simultaneously.

Let it breathe. Call task.wait() before the code in the loop runs again so the engine doesn’t crash.

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()
end

Also, since you’re using the code I provided, I assume it worked; you have to mark posts as solution if they have solved your problem.

1 Like