How would I be able to set a model's CFrame to a position that also changes it's pose?

I made a script that changes a model’s position depending on a variables value. So for example, if the variable has a value of 3, then the model with move to part 3. Basically a five night’s at freddy’s AI, you can search it up to understand more. What I wanna do is depending on the model’s position, it does a different pose. But the only thing I can do is just to move it’s CFrame. How would I be able to make it change the pose depending on the model’s position? Here is the script :

local AI = game.Workspace.abrahamafrica3000
local Door = game.Workspace.Billis.FNaFdoor.OpenDoor

local Positions = {workspace.CheckP1.CFrame,
	workspace.CheckP2.CFrame,
	workspace.CheckP3.CFrame,
	workspace.CheckP4.CFrame,
	workspace.CheckP5.CFrame,
	workspace.CheckP6.CFrame,
	workspace.CheckP7.CFrame,
	workspace.CheckP8.CFrame,
	workspace.CheckP9.CFrame
}

local PositionValue = 1

repeat
	local success = math.random(1, 20)
	print(success)
	task.wait(4)
	if success <= 3 then
		PositionValue += 1

	end

	if PositionValue == 1 then
		AI:SetPrimaryPartCFrame(game.Workspace["abrahamafrica3000(2)"]:GetPrimaryPartCFrame())

	end

	if PositionValue == 2 then
		AI:SetPrimaryPartCFrame(Positions[2])

	end

	if PositionValue == 3 then
		AI:SetPrimaryPartCFrame(Positions[3])

	end

	if PositionValue == 4 then
		AI:SetPrimaryPartCFrame(Positions[4])

	end

	if PositionValue == 5 then
		AI:SetPrimaryPartCFrame(Positions[6])

	end

	if PositionValue == 6 then
		AI:SetPrimaryPartCFrame(Positions[6])

	end

	if PositionValue == 7 then
		AI:SetPrimaryPartCFrame(Positions[7])

	end

	if PositionValue == 8 then
		AI:SetPrimaryPartCFrame(Positions[8])

	end

	if PositionValue == 9 then
		AI:SetPrimaryPartCFrame(Positions[9])
	elseif Door.Value == true then
		wait(3)
		PositionValue = 1
	elseif Door.Value == false then
		if PositionValue == 10 and Door.Value == false then
			local char = game.Players.LocalPlayer.Character
			char:FindFirstChild("Humanoid").Health = 0

		end

	end

until PositionValue == 10

Since you’re storing positions as an array, you can also store the AnimationTracks as an array, then play them as PositionValue advances.

By the way, you can just look up Positions[PositionValue]. That codeblock can be rewritten as:

local AI = game.Workspace.abrahamafrica3000
local Door = game.Workspace.Billis.FNaFdoor.OpenDoor

local Positions = {
    workspace.CheckP1.CFrame,
	workspace.CheckP2.CFrame,
	workspace.CheckP3.CFrame,
	workspace.CheckP4.CFrame,
	workspace.CheckP5.CFrame,
	workspace.CheckP6.CFrame,
	workspace.CheckP7.CFrame,
	workspace.CheckP8.CFrame,
	workspace.CheckP9.CFrame
}

local PositionValue = 1

repeat
	local success = math.random(1, 20)
	print(success)
	task.wait(4)
	if success <= 3 then
		PositionValue += 1
	end

	if PositionValue == 1 then
		AI:SetPrimaryPartCFrame(game.Workspace["abrahamafrica3000(2)"]:GetPrimaryPartCFrame())
    
    elseif PositionValue <= 9 then
        AI:SetPrimaryPartCFrame(Positions[PositionValue])

	elseif Door.Value == true then
		wait(3)
		PositionValue = 1
        
	elseif Door.Value == false then
		if PositionValue == 10 and Door.Value == false then
			local char = game.Players.LocalPlayer.Character
			char:FindFirstChild("Humanoid").Health = 0
		end
	end

until PositionValue == 10

You can also use a numeric for loop to build the Positions array:

local Positions = {}

for i = 1, 9 do
    table.insert(Positions, workspace:FindFirstChild("CheckP" .. i).CFrame)
end
1 Like

Thanks a lot. Really helped me :+1:

1 Like

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