How do I set a character's Position when playing an animation?

So I’ve got a valve turning animation and it plays when the Player Holds down a key for a proximity prompt. I want to know how to place the character in a set position so that the animation looks correctly (for example, if the character is not locked in a certain position, it would just look like they are turning air).
Any tips? I know it has something either to do with Character and .Position or .CFrame but I don’t know how to write the code necessary to lock the character.

Proximity Prompt Script:

local BE = game.ReplicatedStorage:WaitForChild("BindableEvent")
local Collect = game.ReplicatedStorage:WaitForChild("CollectEvent")
local ProximityPrompt = script.Parent
local Valve = script.Parent.Parent
local playerHolding -- debounce

local stopanim = game.ReplicatedStorage:WaitForChild("StopAnimGlobal")

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9396489358"

ProximityPrompt.Triggered:Connect(function(player)
	BE:Fire(player)
end)

function Hold(player)
	local hum = player.Character:WaitForChild("Humanoid")
	local valveAnim = hum.Animator:LoadAnimation(animation)
	if playerHolding then return end
	playerHolding = player
	valveAnim:Play()
	stopanim.Event:Connect(function(player)
		valveAnim:Stop()
	end)
	
	repeat
		Valve.CFrame = Valve.CFrame*CFrame.Angles(-5, 0, 0)
		task.wait(1/40)
	until not playerHolding 
end

function HoldEnded(player)
	if playerHolding == player then
		playerHolding = nil
	end
	stopanim:Fire()
end

Collect.Event:Connect(function(player)
	player.leaderstats.Aether.Value = player.leaderstats.Aether.Value + 10
end)

ProximityPrompt.PromptButtonHoldBegan:Connect(Hold)
ProximityPrompt.PromptButtonHoldEnded:Connect(HoldEnded)

After Tinkering a bit, I found a simple solution

Humanoid Root Part

local HRP = player.Character:WaitForChild("HumanoidRootPart")

Humanoid

local hum = player.Character:WaitForChild("Humanoid")

and, in “While Holding” function to add:

local HRP = player.Character:WaitForChild("HumanoidRootPart")
local hum = player.Character:WaitForChild("Humanoid")

HRP.Position = Root.Position
hum.WalkSpeed = 0

and in “When Stopped Holding” function to add:

local HRP = player.Character:WaitForChild("HumanoidRootPart")
local hum = player.Character:WaitForChild("Humanoid")

hum.WalkSpeed = -- your normal walkspeed

add the “HRP” and “hum” at the start of the function and the hrp position and walkspeed right before the animation:Play() and animation:Stop()

If anybody has an alternative more efficient let me know!

edit: While being simple, it doesn’t also lock the character’s rotation, meaning the player can still rotate if using shiftlock or firstperson

Hi!

So I assume that you know the distance from the keyhole, to where the humanoidRootPart should be located.

I’ve been troubleshooting a bit myself to get this working, but here is what you might be looking for! :slight_smile:

		local CharPos = Character.HumanoidRootPart.Position -- Your HumanoidRootPart.Position
		local PivotPos = PivotPart.Position --The keyhole's position
		local SavedCFrame = CFrame.new(PivotPos.X,CharPos.Y, PivotPos.Z) + PivotPart.CFrame.LookVector*10 -- What we do here is, we create a new CFrame (with the position x & z from our keyhole, but the y axis from our character) and then moves it 10 studs away from the keyhole. You just edit the 10 with the distance you want the animation to play away from the keyhole.
		Character:PivotTo(CFrame.lookAt(SavedCFrame.Position, PivotPart.Position)) -- Now we PivotTo(move our character by the pivotPoint to our saved CFrame's position, and make it look at our PivotPart(keyhole's) position.

Thank you, I’ll look into this! Is there also a solution to make the character’s rotation stay in place? So that the player can’t rotate while doing the action

Anchor the HumanoidRootPart before doing the code I sent you, and then when animation has finished playing, unanchor the HumanoidRootPart again

Thank you so much! I’ll send your answer as a solution right now. Tysm!

Hopefully it works out, feel free to send a short video of the code in action if possible, or another reply if it doesn’t function as you wanted it to.

1 Like

there is a bug, the character teleports away from the valve

Also, Anchoring doesn’t stop Shift-Locking, so I searched up and found the “humanoid AutoRotate” feature. Sadly it only works if I’m not using the prompt. Any way to implement that While using it?

Hi!

So, you gotta make sure that the part you use as PivotPart. That part should have it’s front surface facing the direction you want the player to be. Also the reason why the character teleports away is, that you haven’t changed the “10” to whatever distance you want the character to have, from the machine when they should do the animation. :slight_smile:

Well, you could force Shift-locking not being a thing in general in your game, or do so it isn’t a thing while you do the animation. (The first will be the most reliable option)

I, myself, find not having Shift-Lock in-game is very annoying, cause you have to use Right clicking to move your camera. That’s mainly the reason why I don’t want to remove shiftlock

You gotta find a way to prevent the rotation happening from the shiftlock then. :slight_smile:

1 Like