#Sprint and Crouch animations

1 messages · Page 1 of 1 (latest)

hollow shadow
#

You should be able to put that in startercharacterscripts. Startercharacterscripts puts all of the scripts inside of it inside the player's character when they join. StarterPlayerScripts puts all of the scripts inside of it inside of the actual player.

Personally, I put all my animation stuff inside of the starterCharacter, because thats the more convinent spot where i can easily access the player model. For stuff like GUI's and other client handling, I use starterplayerscripts.

rough nest
violet kelpBOT
#

studio** You are now Level 2! **studio

rough nest
hollow shadow
#

Its been a while since ive used one, Ill check one of my old games to see where it is.

#

May I see your script? I tried it in both starterPlayer and starterCharacter and both seemed to work okay.

rough nest
#

cuz im trying to make a crouch system next and i think it would be similar to the sprint system more or less

hollow shadow
#

Either one will work, but it should still be working in starterplayerscripts, I think that might be a script issue about why its not working after player death

rough nest
# hollow shadow Either one will work, but it should still be working in starterplayerscripts, I ...
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
repeat wait() until Character:FindFirstChild("Humanoid")
local humanoid = Character.Humanoid
local SprintState = Instance.new("BoolValue")
SprintState.Name = "SprintState"
SprintState.Parent = Character
local running = false

local function handleSprint(shouldSprint)
    if shouldSprint == true then
        humanoid.WalkSpeed = 22
        running = true
    else
        humanoid.WalkSpeed = 10
        running = false
    end
    SprintState.Value = running
end

local function handleInput(actionName,inputState,inputObject)
    if actionName == "Sprint" then
        if inputState == Enum.UserInputState.Begin then
            handleSprint(true)
        elseif inputState == Enum.UserInputState.End then
            handleSprint(false)
        end 
    end
end

local function listenInput()
    ContextActionService:BindAction("Sprint",handleInput,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL1)
    ContextActionService:SetTitle("Sprint", "Sprint")
    ContextActionService:SetPosition("Sprint", UDim2.new(1, -70, 0, 10))
end

listenInput()

this is my script for the sprinting system, also for keybinds is it better for characterscript or playerscript

hollow shadow
#

hmm... is their any errors in the output when you die?

#

for the keybinds, its probably better to be in starterplayscripts bc its more direct to the player but both should still work in either one

rough nest
#

but it didnt work when i respawned while it was in starterplayer but moving it to startercharacter i can sprint after respawning

#

and leftshift also seems to work

hollow shadow
rough nest
hollow shadow
#

in terms of functionality their isint a difference

#

its just the location

#

both have purposes for different things

rough nest
hollow shadow
#

Well like i said, its all about the locations. For example, in my game, I do all the GUI stuff in starterplayerscripts. Because it has much better access to plrGui. If it was in startercharacterscripts, It would still work, but it would take more effort and it woulden't work as well.

violet kelpBOT
#

studio** You are now Level 4! **studio

hollow shadow
#

Also I just tried you script out and reset my character, and it worked fine for me, not sure what the issue could be on your end.

hollow shadow
#

Startercharacter

rough nest
#

yea put it in starterplayer

#

i had it there and didnt work, but moving it to startercharacter fixed it

#

cuz u said u put it in startercharacter

hollow shadow
#

Oh I see

rough nest
#

yea i didnt understand that lol

hollow shadow
#

Yeah now its not working

hollow shadow
#

I have a feeling it has to with something about the running boolvalue i will investigate it

rough nest
hollow shadow
#

I found the issue

#

When the plr dies, everything in their character is reset. For some reason, and i wasan't really aware of this, it dosen't add back any bool values put inside of the character.

rough nest
#

so the bool part was the problem right

violet kelpBOT
#

studio** You are now Level 3! **studio

rough nest
#

what did u do to fix that

hollow shadow
#

Yes i belive so

#

So you could either check when the character dies and make it again OR you could move the value to the player instead of the character and it shoulden't be destroyed.

#

Moving it to the character would probably be easiar

rough nest
hollow shadow
#

You dont have to, I mean just setting the bool value's parent to the player, not player.character

#

Also, you will have to redefine the humanoid as since your defining it at the start of your script so when the player dies the old humanoid wont exist anymore. You can fix it by just defining it when the HandleSprint function runs.

rough nest
#

ok

rough nest
# hollow shadow Also, you will have to redefine the humanoid as since your defining it at the st...

im trying to put the animation and the sprint in one script and ive done this and my animation is not playing anymore,

local Players = game:GetService("Players")
local CAS = game:GetService("ContextActionService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")

local animation = script:WaitForChild("AnimationRun")
local animationTrack = humanoid:LoadAnimation(animation)
local running = false

local function isMoving()
    return humanoid.MoveDirection.Magnitude > 0
end

local function sprint()
    if running and isMoving() == true then
        animationTrack:Play()
        animationTrack:AdjustSpeed(1.35)
    else
        animationTrack:Stop()
    end
end

humanoid.Running:Connect(function(speed)
    if speed == 0 then
        animationTrack:Stop()
    elseif running and not animationTrack.IsPlaying then
        animationTrack:Play()
        animationTrack:AdjustSpeed(1.35)
    end
end)

local function handleSprint(shouldSprint)
    if shouldSprint == true then
        humanoid.WalkSpeed = 22
    else
        humanoid.WalkSpeed = 10
    end
end

local function handleInput(actionName,inputState,inputObject)
    if actionName == "Sprint" then
        local shouldSprint = false
        if inputState == Enum.UserInputState.Begin then
            shouldSprint = true
        end
        handleSprint(shouldSprint) 
        sprint()
    end
end

local function listenInput()
    CAS:BindAction("Sprint",handleInput,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL1)
    CAS:SetTitle("Sprint", "Sprint")
    CAS:SetPosition("Sprint", UDim2.new(1, -70, 0, 10))
end

listenInput()
hollow shadow
#

Not sure if this will work but try doing if not running on line 19

#

@rough nest

rough nest
#

line 19 is the speed

#

@hollow shadow

#

u want me to put it on 19?

hollow shadow
#

Oh mb I mean line 17

#

it was different for me

#

so just put a not inbetween if and running

#

because what i think is happening is the running value is swapped around but im not sure so this is just to check

rough nest
#

wow ok that did fix it but now i have 2 different bugs, one is that when i start sprinting normally it works but after i let go of wasd but still keep holding leftshift and then press wasd again the animation doesnt play

#

and the other one is the opposite, if i let go of shift but hold down wasd then the animation still plays

rough nest
hollow shadow
#

For the second one you should just be able to do animationTrack:Stop() in the else statement of your HandleScript function

#

HandleSprint()*

rough nest
rough nest
hollow shadow
#

Yeah it gets kind of confusing. What i did to find this out was to print the value of running and when I pressed left shift it was returning false, so somehow its inverted and when your sprinting running is actually false and when your not its true.

rough nest
#

tahts strange cuz running is suppose to be false and ismoving is suppose to have magnitude>0

hollow shadow
#

You might have to add back in your inputState == Enum.UserInputState.End because thats the only way i could think of to make sure the animation stops when they let go

#

And u dont need to make a function for it just add it as an elseif with your if statement

#

And you will also need to move the sprint function inside of the first if condition

hollow shadow
#

Yes put the elseif there

rough nest
#

yes i have it like this now

local function handleInput(actionName,inputState,inputObject)
    if actionName == "Sprint" then
        local shouldSprint = false
        if inputState == Enum.UserInputState.Begin then
            shouldSprint = true
        elseif inputState == Enum.UserInputState.End then
            shouldSprint = false
        end
        handleSprint(shouldSprint) 
        sprint()
    end
end

but wdym by u dont need to make a function for it

hollow shadow
#

Idk why i said that lol I was just making sure. Anyway now you would want to put your sprint() inside of the first if inputState and then in the elseif you want to do AnimationTrack:Stop()

rough nest
#

so u want me to put sprint() under shouldSprint=true?

hollow shadow
#

Yes, because then if the input ends it wont call sprint

rough nest
#

but the bug i still have the bug where i let go of wasd but hold down shift still and then press wasd then my animation doesnt play

rough nest
rough nest
hollow shadow
#

This sounds weird but add i think you need to add running = true

rough nest
#

the elseif part

rough nest
#

under the elseif?

hollow shadow
#

if speed == 0 then

rough nest
#

wow that did fix it

#

but i dont understand how

hollow shadow
#

Lets go i had a good feeling about it

rough nest
#

how did u know

hollow shadow
rough nest
#

yes but i dont even understand how its inverted

#

i have it straight out = false

hollow shadow
#

When the plr stops running, speed returns zero. So thats a good way to detect if theyve stopped. And your running variable controls whether the animation plays or not. So when they stopped nothing was changing the running variable so it was still false, meaning that it still thought the plr was running

hollow shadow
#

Can u send me your whole script again

rough nest
# hollow shadow Can u send me your whole script again

this is my current script

local Players = game:GetService("Players")
local CAS = game:GetService("ContextActionService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")

local animation = script:WaitForChild("AnimationRun")
local animationTrack = humanoid:LoadAnimation(animation)
local running = false

local function isMoving()
    return humanoid.MoveDirection.Magnitude > 0
end

local function sprint()
    if not running and isMoving() == true then
        animationTrack:Play()
        animationTrack:AdjustSpeed(1.35)
    else
        animationTrack:Stop()
    end
end

humanoid.Running:Connect(function(speed)
    if speed == 0 then
        running = true
        animationTrack:Stop()
    elseif running and not animationTrack.IsPlaying then
        animationTrack:Play()
        animationTrack:AdjustSpeed(1.35)
    end
end)

local function handleSprint(shouldSprint)
    if shouldSprint == true then
        humanoid.WalkSpeed = 22
    else
        humanoid.WalkSpeed = 10
        animationTrack:Stop()
    end
end

local function handleInput(actionName,inputState,inputObject)
    if actionName == "Sprint" then
        local shouldSprint = false
        if inputState == Enum.UserInputState.Begin then
            sprint()
            shouldSprint = true
        elseif inputState == Enum.UserInputState.End then
            shouldSprint = false
            animationTrack:Stop()
        end
        handleSprint(shouldSprint) 
    end
end

local function listenInput()
    CAS:BindAction("Sprint",handleInput,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL1)
    CAS:SetTitle("Sprint", "Sprint")
    CAS:SetPosition("Sprint", UDim2.new(1, -70, 0, 10))
end

listenInput()
violet kelpBOT
#

studio** You are now Level 4! **studio

rough nest
#

oh actually i found another bug, now the shift just toggles the animation on forever

#

so if i started sprinting once, the animation will play regardless on how u move

#

like even without holding it

#

even after respawning the animation plays

hollow shadow
#

Wait so does that happen when you press shift while your not moving

rough nest
#

nothing happens if im not moving

#

but if i press shift once

#

then the animation toggles on forever

#

wait nvm

#

i dont even need the shift

#

the animation just runs when i move regardless

hollow shadow
#

what the heck? So now when you join its straight up just running without pressing shift?

violet kelpBOT
#

studio** You are now Level 5! **studio

rough nest
#

yea the animation plays whenever i move

#

even if im not sprinting

hollow shadow
#

oof

#

alr ill look into that

rough nest
#

like even while walking in basespeed the animation plays

hollow shadow
#

oh i think i already found it

#

in your humanoid.running function

#

add a not in between the elseif and the running

hollow shadow
#

yeah in that part

rough nest
#

so it would be elseif not running and not animationTrack.IsPlaying

#

ok

hollow shadow
#

Correct

rough nest
#

now the animation doesnt play at all

hollow shadow
#

Bc we havent fixed the running being inverted yet

rough nest
#

even when sprintin

hollow shadow
#

Uh oh

rough nest
#

how are u so good at bug fixing lol

hollow shadow
#

lol thanks sometimes it just takes another person to look at it, sometimes i need help on stuff I wrote that I stopped understanding.

#

hmmm

#

Can you just add this before that if statement: print(running, animationTrack.IsPlaying), both should return true in the output

rough nest
#

do i keep the not

#

or remove that

hollow shadow
#

keep it for now

#

unless running is returning false

rough nest
hollow shadow
#

Interesting

rough nest
#

so how come the running is false now i thought it was inverted or something

#

and hows it twice

hollow shadow
#

Honestly maybe just replace the running variable with this: speed ~= 0

#

Bc i know for a fact that will retrn true if they are moving

rough nest
#

so local speed ~= 0

hollow shadow
#

no no no I meant in the if statement where you have the elseif running and not animationTrack.IsPlaying

rough nest
#

elseif not speed ~= 0 and not animationTrack.IsPlaying then?

#

like that?

#

but not speed ~= 0 is underlined

hollow shadow
#

And remove the first not

rough nest
#

oh

#

now the animation plays no matter what again

hollow shadow
#

oh wait i know what i did im dumb

#

yeah that will play regaurdless if they are sprinting or not

#

ummm

#

ok replace the speed ~= 0 with humanoid.WalkSpeed = 22

#

Not the most efficient way to do it but I just want to identify the problem

rough nest
#

= shows as an error do u mean ==?

hollow shadow
#

whoops yeah mb

rough nest
#

ok yea this fixes everything

rough nest
hollow shadow
#

Well since the speed of your sprint is 22 all that is doing is checking if thats the speed when they are running so then it will start playing the animation when it is

#

Any bugs?

rough nest
#

not that i can see so far no

hollow shadow
#

great

rough nest
hollow shadow
#

Oh sure, sorry i got this so late

#

let me know what i can help with

rough nest
# hollow shadow let me know what i can help with

sorry i didnt see this till now, when i press the keybind to crouch then the full animation plays the whole time, but when im walking and holding C then its normal but when i just press C then the animation still plays

hollow shadow
rough nest
hollow shadow
#

hmm that’s weird can I see the code