Script doesn't work when I disable/enable it

Alright, I’ve got 2 scripts, I took them from toolbox and altered a bit, anyway, here they are
This one makes the NPC follow the player

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

	
	function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait()
	local player = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if player ~= nil then
		script.Parent.Humanoid:MoveTo(player.CFrame * CFrame.new(3, 0, 2).Position, player)
		

	
	end

end

And this one makes it follow the enemy (zombie in this case)

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

	function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Zombie")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait()
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
		
		
	
		
	end

end

And I want to make a system in which when player presses F, the FollowEnemy script gets disabled and FollowPlayer script gets enabled and vice versa, when player presses G the FollowPlayer script gets disabled and FollowEnemy gets enabled, I already wrote a script like that and it works as it should, but these two scripts just don’t want to work after getting enabled

Help?

In the while true do function, try adding a boolvalue child in the script and change it to while (script.bool) do
To change the bool value, add another script and a remote event where the remote event gets triggered with the local script and when the script detects the remote, it changes the bool

if your confused just reply

1 Like

Hell yeah! Works like a charm now, turns out it was so obvious, even for a complete amateur like me. Thank you! Small addition: I also had to add while true do before while bool.value == true do so the value always gets checked (script wouldn’t work without it)

1 Like