Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Random Jump animation is not working?

Asked by 4 years ago
animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620182994"  -- Jump
animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620201065"  -- Jump2

This is the part of the code that isn't working. Roblox Developer Wiki says that it should be random, but it only plays 1 the entire time. I want it to play one or the other at random. Help!

0
Make 2 animations and put it in a folder then randomize it by writing a script. HKprogram 48 — 4y
0
I've tried that already, it only chooses one animation and then sticks with it the entire time Sheepsee 2 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You can simply use math.random().

while wait() do 
    local random = math.random(1, 2)
    if random == 1 then
        animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620182994"
    else
        animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620201065"
    end
end

Lemme know if it works!

0
Each time you reset or get on the game, it has a random jump animation, but then it just sticks with that one animation Sheepsee 2 — 4y
0
So, it sticks with animation of Jump1? BestCreativeBoy 1395 — 4y
0
Yeah, or it sticks with Jump2 by random Sheepsee 2 — 4y
0
Try adding while wait(youtTime) do loop BestCreativeBoy 1395 — 4y
View all comments (5 more)
0
youtTime? Sheepsee 2 — 4y
0
I mean *yourTime, in simple words, the value in seconds for running the loop again BestCreativeBoy 1395 — 4y
0
yes! it works! i did make an edit to the script because it actually would change to the default animation, edit the post for other people: Sheepsee 2 — 4y
0
while wait() do local random = math.random(1, 2) if random == 1 then animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620182994" else animateScript.jump.JumpAnim.AnimationId = "rbxassetid://5620201065" end end Sheepsee 2 — 4y
0
the anim variable makes it default to the normal jump animation Sheepsee 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You can check for player state when the player jumps. If the new state is Enum.HumanoidStateType.Jumping, they're jumping! This means you can reselect another animation randomly each time they jumps.

Use LocalScript for this.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() --Waits for their character to get their humanoid

local hum = char:WaitForChild('Humanoid') --again, wait for their humanoid if it's not loaded yet

local anims = {
    "rbxassetid://5620182994",
    "rbxassetid://5620201065"
}

local animObj = animateScript.jump.JumpAnim --Your animation object here.

local changeAnimation = {
    [Enum.HumanoidStateType.Jumping] = true
    --You can add more states here if you want multiple states to run animation rotation.
    --If you don't want any more states, use Humanoid.Jump event instead.
}

--Humanoid.StateChanged -> (oldState, newState)
--no need oldState for this
hum.StateChanged:Connect(function(_, newState)
    --Use dictionary indexing to find if this is the state you want the animation to be updated the next time you jump.
    if changeAnimation[newState] then
        --Use math.random!
        animObj.AnimationId = anims[math.random(1, #anims)] --This gets one animation id and update the AnimationId property.
    end
end)

Just to make sure, you should use humanoid:LoadAnimation(animationObject) in case you want to play this animation yourself when the player jumps.

Answer this question