Change Player Speed By Using Animation Events

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so you stand still while using Melee Attacks.

  2. What is the issue? Include screenshots / videos if possible!
    It does not work, and it may be a bit more difficult since it’s 3 Animations, not just one.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I checked, but there is no solution for mine.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local PlayerSpeed = script.Parent.Parent.Humanoid.WalkSpeed


function Movement(AnimationTrack, PlayerSpeed)
	AnimationTrack:GetMarkerReachedSignal("ResumeMove"):Connect(function()
		PlayerSpeed = 16
	end)
	AnimationTrack:GetMarkerReachedSignal("StopMove"):Connect(function()
		PlayerSpeed = 0
	end)
end

This is how it looks like ingame:
image
“fruitloop” is my displayname
“Behavior” is the script that should change the PlayerSpeed on Animation Events.
image
Those are my Animations, that should be compatible!
If you need any more detail, please ask! :slight_smile:
I’m still very new to scripting! so please do criticize! and give feedback!

3 Likes

You’re able to create events that fire in a time position, to which you can set:

Animation Events | Documentation - Roblox Creator Hub.

Edit: I just realised you did use that. Sorry for not reading it correctly.

1 Like

Honestly, I don’t see any errors in your code but I guess you could add a check to see if an animation is a melee animation by using attributes etc.

But also try changing the humanoid walk speed directly:

local Humanoid = script.Parent.Parent:WaitForChild("Humanoid"):: Humanoid

-- code
Humanoid.WalkSpeed = 16

It really shouldn’t make a difference but hey maybe it will!

Edit: Works for me now so :man_shrugging: (updating walkspeed directly)

3 Likes

Is the function movement() being ran? If not, the actual animation events won’t be running.

2 Likes

its fine! im just really confused why it does not work :sob:
i tried overwriting the animations, maybe they didnt apply the events or something.
but no success.

2 Likes

oh wait no! how do i do that? Cause i have no clue on how it works!

2 Likes

maybe we can make it function when the player equips the tool? , when unequipped it should stop working then!

1 Like

Connect it to when the animation is played. There’s an event that’s fired when an animation is played so you need to connect it to that, see: Using Animations - create.roblox.com.

However, you don’t need this in a function, you can just reference the animation and do those calculations from there, see the website linked above for more info about that.

2 Likes

You need to change Humanoid.WalkSpeed instead of changing the variable PlayerSpeed, cuz changing just the variable won’t affect Humanoid.WalkSpeed.

Also, make sure that before playing the animation, the AnimationPriority is set to Action4 cuz walking/jumping might stop the animation from playing.

AnimationTrack.Priority = Enum.AnimationPriority.Action4

For more info about AnimationPriority check this AnimationTrack | Documentation - Roblox Creator Hub

2 Likes

The variable is literally referencing to humanoid.WalkSpeed, that’s not how that works.

Changing the variable will affect the walkspeed.

2 Likes

You’re wrong. Variables only affect itself when changed (unless used as a debounce or bool). Just because you set a property of an Instance as a variable, doesn’t mean it affects the property if the variable is changed.

Look at this sample script:

local speed = Humanoid.WalkSpeed
print(speed) -- prints 16 (default)

speed = 32

print(speed) -- prints 32
print(Humanoid.WalkSpeed) -- prints 16 (default)

As you can see, changing the variable only changes the number of the variable speed for the rest of the script. It doesn’t change `Humanoid.WalkSpeed.

To actually change Humanoid.WalkSpeed, you must actually type that and set it to whatever speed you want.

You can actually try this in Studio. You will see why you’re wrong.

The variable in your example is just speed, what he used is humanoid.WalkSpeed, changing the variable to that changes the walkspeed. You should read the code next time:

local variable = humanoid.WalkSpeed

variable = 32

print(humanoid.WalkSpeed)
--32

it doesn’t matter if the H is small or capital :face_with_diagonal_mouth:, either way it’s referring to the Class Humanoid