Why won't my script work?

Just a couple hours ago this script was working by going to a player that is moving.
Now, I went into my game, and it isn’t working anymore.

Anyone know why?

(There are no errors in output, the script doesn’t seem to be executing)

local range = script.Configuration["Range (IN STUDS)"].Value
local rootpart = script.Parent.HumanoidRootPart
local anim = script.Parent.Partygoer.Humanoid:LoadAnimation(script.Parent.Partygoer.Humanoid.Chase)

game:GetService("RunService").Heartbeat:Connect(function()
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v:IsA("Humanoid") then
			local WS = v.WalkSpeed

			if WS > 4 then
				if v.MoveDirection == Vector3.new(0,0,0) then
					anim:Stop()
					script.Parent.HumanoidRootPart.Theme.DistortionSoundEffect.Enabled = false
					script.Parent.Humanoid.WalkToPoint = script.Parent.OriginalPos.Position
				else
					local pos = v.Parent:FindFirstChild("HumanoidRootPart").Position
					local mag = (pos-rootpart.Position)
					local mag2 = mag.Magnitude

					if mag2 < range then
						if anim.IsPlaying == false then
							anim:Play()
						end
						script.Parent.Humanoid.WalkToPoint = pos
						script.Parent.HumanoidRootPart.Theme.DistortionSoundEffect.Enabled = true
					else
						anim:Stop()
						script.Parent.HumanoidRootPart.Theme.DistortionSoundEffect.Enabled = false
						script.Parent.Humanoid.WalkToPoint = script.Parent.OriginalPos.Position
					end
				end
			else
				anim:Stop()
				script.Parent.HumanoidRootPart.Theme.DistortionSoundEffect.Enabled = false
				script.Parent.Humanoid.WalkToPoint = script.Parent.OriginalPos.Position
			end
		else
			anim:Stop()
			script.Parent.HumanoidRootPart.Theme.DistortionSoundEffect.Enabled = false
			script.Parent.Humanoid.WalkToPoint = script.Parent.OriginalPos.Position
		end
	end
end)

Specify which line failed please.

All of them, there were no errors in output. It just isn’t executing.

I can’t find any problems with your code. Try using print() on the if statements to see where the execution fails.

Example:

function Something(param1, param2, param3)
	if param1 == true then
		print("1") -- Something like this
		if param2 == true then
			print("2") -- Place print functions after the if statement.
			if param3 == true then
				print("3") -- One every statement should be good.
				print("yep it works")
			end
		end
	end
end

Something(true, true, true)
-- 1
-- 2
-- 3
-- yep it works

Something(true, false, true)
-- 1
-- Execution stops here since param2 ~= true.

Tried that, nothing ever popped up on it

Is this a script or localscript, and where is it located when its trying to run, and make sure it is not disabled.

How is your model set up? Mind if you send a screenshot of your Partygoer’s model hierarchy (in Explorer)?

image

This is a script, this is running in a custom character, it is not disabled.

It appears that for every Humanoid your script detects, it would start moving, but since the loop is not finished and has found another Humanoid from afar or not moving, it will also stop. Try this:

local range = script.Configuration["Range (IN STUDS)"].Value
local rootpart = script.Parent.HumanoidRootPart
local anim = script.Parent.Partygoer.Humanoid:LoadAnimation(script.Parent.Partygoer.Humanoid.Chase)

local hrp = script.Parent.HumanoidRootPart
local partygoerHumanoid = script.Parent.Humanoid
local originalPos = script.Parent.OriginalPos

game:GetService("RunService").Heartbeat:Connect(function()
	local closestRoot = nil
	
	for i, v in ipairs(workspace:GetDescendants()) do
		if v:IsA("Humanoid") then
			local humanoid = v :: Humanoid -- Tell autocomplete that this variable is a Humanoid.
			local root = humanoid.RootPart or humanoid.Parent:FindFirstChild("HumanoidRootPart")
			local walkspeed = humanoid.WalkSpeed
			local direction = humanoid.MoveDirection
			
			if not root then
				continue -- Continue skips the rest of the loop.
			end
			
			-- Your original code also detects the Partygoer as a target.
			if (humanoid == partygoerHumanoid) then
				continue
			end
			
			-- Do you also need to check Health?
			if humanoid.Health == 0 then
				continue
			end
			
			local humanoidPosition = root.Position
			local distance = (hrp.Position - humanoidPosition).Magnitude
			
			if distance < range then
				if walkspeed > 4 and not (direction == Vector3.zero) then
					closestRoot = root
				end
			end
		end
	end
	
	if not closestRoot then
		if anim.IsPlaying then
			anim:Stop()
		end
		
		hrp.Theme.DistortionSoundEffect.Enabled = false
		partygoerHumanoid.WalkToPoint = originalPos.Position
	else
		if not anim.IsPlaying then
			anim:Play()
		end

		hrp.Theme.DistortionSoundEffect.Enabled = true
		partygoerHumanoid.WalkToPoint = closestRoot.Position
	end
end)
2 Likes

The one thing I forgot lol. I appreciate it!

2 Likes