I’m trying to do running using Context Action, before that I didn’t use it at all and now I wanted to learn and do running, but it doesn’t work
I am not a professional in scripting myself
There are no errors in the code, at least there are no errors in the output
I haven’t tried any attempts to fix it, well, I searched for something on the Internet but I didn’t find anything
Вот код:
1 Like
Are you sure the character has spawned in when you defined it?
I didn’t understand the question, I press F5 and the character appears and this script also appears inside
I rewrote your script with the best practices I know right now.
-
Waiting for all the components to load and saving them into a variable.
→ Humanoid might not be loaded right away, therefore use :WaitForChild()
.
-
Load animations onto the animator (once!), and play/stop the created animation.
→ humanoid:LoadAnimation() is deprecated.
-
Using Enums.
-
Connecting humanoid.Running˙
to check whether the player is moving and playing/stopping animation accordingly.
I used the default Roblox walk animation, because your animation won’t play as it wasn’t me who uploaded it. The proper format for custom animations is rbxassetid://id_here
.
The rest of the script should be self explainatory. If you are using a non-English keyboard like QWERTZ, Z is actually Y.
local CAS = game:GetService("ContextActionService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
-- animation.AnimationId = "rbxassetid://13786314467"
animation.AnimationId = "http://www.roblox.com/asset/?id=507777826"
local runAnim = animator:LoadAnimation(animation)
runAnim.Priority = Enum.AnimationPriority.Action
runAnim.Looped = true
local is_sprinting = false
local function OnSprintRequest(actionName, inputState, inputObject)
if inputObject.KeyCode == Enum.KeyCode.Z then
if inputState == Enum.UserInputState.Begin then
is_sprinting = true
humanoid.WalkSpeed = 100
end
elseif inputObject.KeyCode == Enum.KeyCode.X then
if inputState == Enum.UserInputState.Begin then
is_sprinting = false
humanoid.WalkSpeed = 16
runAnim:Stop(.2)
end
end
end
humanoid.Running:Connect(function(speed)
if is_sprinting and speed ~= 0 and not runAnim.IsPlaying then
runAnim:Play()
elseif speed == 0 and runAnim.IsPlaying then
runAnim:Stop(.2)
end
end)
CAS:BindAction("Sprint", OnSprintRequest, false, Enum.KeyCode.Z, Enum.KeyCode.X)