Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my mobs get glitched when a target [Player] leaves the game?

So in this script, i made it so when a player is in range of the mob, it will try and hunt down the player. But when a player leaves they get glitched and will stay still. They still do damage though

local pathfind = game:GetService("PathfindingService")
local statsfolder = script.Parent:WaitForChild("STATSFOLDER")
local humanoid = script.Parent:WaitForChild("Humanoid")
local mindamage = statsfolder:WaitForChild("MinDamage")
local maxdamage = statsfolder:WaitForChild("MaxDamage")
local visiondistance = statsfolder:WaitForChild("VisionStud")
local targetvalue = statsfolder:WaitForChild("Target")
local position = statsfolder:WaitForChild("Position")
local torso = script.Parent:WaitForChild("Torso")
local enabled = false
math.randomseed(tick()-wait(1))

--Damaging Player

torso.Touched:connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") and part.Parent.Name ~= "NPC" and enabled == false then
		enabled = true
		local humanoid = part.Parent:WaitForChild("Humanoid")
		humanoid:TakeDamage(math.random(mindamage.Value, maxdamage.Value))
		wait(2)
		enabled = false
	else
		enabled = true
		humanoid.Jump = true
		wait(1.5)
		enabled = false
	end
end)

function FindHumanoid()
	local allplayers = game.Players:GetPlayers()
	for i, v in pairs(allplayers) do
		if v.Character and v.Character:FindFirstChild("Humanoid").Health > 0 then

			local targettorso = v.Character:FindFirstChild("Torso")
			local location = (torso.Position - targettorso.Position).magnitude
			if location < visiondistance.Value then
				targetvalue.Value = v.Character
			else			
			end
		end
	end
end

--If player health is higher then 0 then they follow the player

while humanoid.Health > 0 do
	FindHumanoid()
	if targetvalue.Value then
		local rtorso = targetvalue.Value:FindFirstChild("Torso")
		if rtorso then
			local path = pathfind:ComputeSmoothPathAsync(torso.Position, rtorso.Position, 200)
			local check = path:GetPointCoordinates()
			for i = 1, #check do
				local singularpoints = check[i]
				humanoid:MoveTo(singularpoints)
			end
		else		
		end
	else
		
	end
	wait(0.5)
end

1 answer

✓ Accepted answer

What type is TargetValue? Int or String?

What I recommend doing is instead of setting a another value to the character, return the character from the function call

Instead of targetvalue.Value = v.Character

Do: return v.Character

This is better as you can check if the player is in the game aswell. In the call FindHumanoid() add:

local char = FindHumanoid()

To hold the returned value. Then you can do checks on whether the char.Parent and or char exists. If it doesn't, repeat the call to FindHumanoid()

2 comments

0

Of course you will need to edit your code to use the returned value instead

0

Oh it works, thanks alot! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.