I saw several similar threads but couldn’t find the solution to this simple situation: I want to anchor the character while it’s running, by pressing P:
Run the script below
Start running with the character (key W)
While running, press P (will anchor the character)
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
UserInputService.InputBegan:Connect(function(Input,GPE)
print(Input.KeyCode)
if Input.KeyCode == Enum.KeyCode.P then
Player.Character.PrimaryPart.Anchored = true
end
end)
But the running animation won’t stop:
How to stop character running animation?
Edit
I’ve also tried this, but no success:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(Input,GPE)
print(Input.KeyCode)
if Input.KeyCode == Enum.KeyCode.P then
print("Humanoid:GetState(): ", Humanoid:GetState())
Humanoid:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics, false)
Player.Character.PrimaryPart.Anchored = true
end
end)
I mean you could always make an animation using studio’s animation software thing. Then make an animation where the character is just doing nothing. Then you can make it so that it plays that animation where it is in place. There’s probably an easier method but uh ye.
This is actually a very good question. My only thought is before anchoring the HumanoidRootPart, set the walk speed to 0, and add a very small delay, then anchor. This should stop the character from continuing to walk, giving time for the animation to stop. Let me know how this works.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(Input,GPE)
print(Input.KeyCode)
if Input.KeyCode == Enum.KeyCode.P then
for index, part in pairs(Character:GetChildren())do
if(part:IsA("Part"))then
part.Anchored = true
end
end
end
end)
The one above only worked for r6, but this one works for all body types now
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(Input,GPE)
print(Input.KeyCode)
if Input.KeyCode == Enum.KeyCode.P then
for index, part in pairs(Character:GetChildren())do
if(part:IsA("Part")) or (part:IsA("MeshPart"))then
part.Anchored = true
end
end
end
end)
Made a new boolean variable that checks the players state. When they press P it freezes, and then P again unfreezes.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
--New variable
local isFrozen = false
UserInputService.InputBegan:Connect(function(Input,GPE)
if (Input.KeyCode == Enum.KeyCode.P) and (not isFrozen)then
for index, part in pairs(Character:GetChildren())do
if(part:IsA("Part")) or (part:IsA("MeshPart"))then
part.Anchored = true
end
end
isFrozen = true
elseif (Input.KeyCode == Enum.KeyCode.P) and (isFrozen) then
for index, part in pairs(Character:GetChildren())do
if(part:IsA("Part")) or (part:IsA("MeshPart"))then
part.Anchored = false
end
end
isFrozen = false
end
end)
After many tests, I think I managed how to create a workaround for this.
It’s not perfect, but it’s a start:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(Input,GPE)
print(Input.KeyCode)
if Input.KeyCode == Enum.KeyCode.P then
Player.Character.PrimaryPart.Anchored = true -- will not stop the running animation
wait(0.5)
Player.Character.PrimaryPart.Anchored = false -- will stop it
wait(0.5)
Player.Character.PrimaryPart.Anchored = true -- back to true after stopping the animation
end
end)
I would recommend stopping the walk speed at first then anchoring the humanoid root part what ever is on top is what is starting first so you don’t need to add waits.
Also recommend adding variables to make it easier.