Prevent npc from repeating the same action twice in a row

Tried to create npc with unpredictable pattern using random Number but I don’t want npc to just standing still 2 or 3 time in a row or chasing non stop.
how do I prevent that?

function Main()
	if Stunned then return end
	Attacked = false
	Tracking = false
	IsChasing = false
	task.wait(DownTime)
	local newAction = math.random(1,3)
	if newAction == OldAction then
		Main()
		return
	end
	if newAction == 1 then
		IsChasing = false
		pcall(Stare)
	elseif newAction == 2 then
		IsChasing = true
		pcall(Chase)
	elseif newAction == 3 then
		IsChasing = false
		pcall(Range)
	end
	OldAction = newAction
end

maybe repeating until u get a different value:

function Main()
	if Stunned then return end
	Attacked = false
	Tracking = false
	IsChasing = false
	task.wait(DownTime)

	local newAction
	repeat
		newAction = math.random(1, 3)
	until newAction ~= OldAction -- keeps randomizing until different value

	if newAction == 1 then
		IsChasing = false
		pcall(Stare)
	elseif newAction == 2 then
		IsChasing = true
		pcall(Chase)
	elseif newAction == 3 then
		IsChasing = false
		pcall(Range)
	end

	OldAction = newAction
end

1 Like

Could you provide some more information, your script already ensures that the actions aren’t repeated:
image

Here’s the code I used to get this result:

local OldAction = 0
local NewAction = 0
local IsChasing = false

function Main()
	
	NewAction = math.random(1,3)
	
	if NewAction == OldAction then
		Main()
		return
	end
	if NewAction == 1 then
		IsChasing = false
	elseif NewAction == 2 then
		IsChasing = true
	elseif NewAction == 3 then
		IsChasing = false
	end
	
	warn(NewAction)
	OldAction = NewAction
end

while task.wait(1) do
	Main()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.