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

Why is my zombie only going straight ahead when it's supposed to go randomly?

I suddenly had an urge to make a zombie script.

Everything worked fine, the zombie went after the player, if the player was close but... If the zombie is more than 50 studs (Robloxes metric system thingy) then it will go random but instead it just goes straight ahead even though i did not use lookvector or anything similar.

Here's the script;

local Humanoid = script.Parent.Humanoid -- referencing building blocks of the zombie
local Torso = script.Parent.HumanoidRootPart

local RandomX = math.random(10, 20) -- Calculating a random path for later.
local RandomZ = math.random(10, 20)

local function Players() -- function
	for i, player in pairs(game.Players:GetPlayers()) do -- gets all the player
		if player.Character then -- checks if there is a character
			local Character = player.Character -- defines the character
			local Torso1 = Character:FindFirstChild("HumanoidRootPart")
			
			if (Torso.Position - Torso1.Position).magnitude <= 50 then -- if the player is 50 studs close to the zombie it will return the player.
				return player
			end
		end
	end
end

while wait() do -- while it waits it will do
	local player = Players() -- we define the player again
	
	if player then -- if there is a player then
		Humanoid:MoveTo(player.Character.HumanoidRootPart.Position) -- it will move to the players humanoid root part
	else
		Humanoid:MoveTo(Vector3.new(RandomX, 2, RandomZ)) -- it will move the zombie to a random position.
	end
end

1 answer

✓ Accepted answer

you're not recalculating the random each time so the zombie will always move to the same position.


if not player blah blah blah then
     huamnoid:MoveTo(Random.new():NextNumber(10,30), 2, Random.new():NextNumber(10,30))
end

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