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

How do I make it to where if you press C a certain amount of times, it does something?

Asked by 3 years ago

This is all I have right now, I got help from some websites n stuff. It makes you crouch, and that's it. You can stand back up, but that's all it does. I'm wanting it to be like: C pressed once = Crouch C pressed twice = Lay C press three times = Stand back up again

local key = "C"
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimationID = "6974258690"
local CharAnimation
local ContextActionService = game:GetService("ContextActionService")
local FREEZE_ACTION = "freezeMovement"


local Sens = 0.12
local LastPressed = nil
game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.C then
        local Trig = false
        if LastPressed ~= nil then
            if tick() - LastPressed <= Sens then
                print("Open Menu")
                Trig = true
            end
        end
        if not Trig then
            LastPressed = tick()
        end
    end 
end)


game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode[key] then
        animation()
    end
end)
function animation()
    if Character then
        local idleanimation = Character:FindFirstChild("AnimationCharacter")
        if CharAnimation then
            CharAnimation:Stop()
            ContextActionService:UnbindAction(FREEZE_ACTION)
        end
        if idleanimation then
            if idleanimation.AnimationId=="rbxassetid://"..AnimationID then
                idleanimation:Destroy()
                return
            end
            idleanimation:Destroy()
        end
        local Animation =Instance.new("Animation",Character)
        Animation.Name= "AnimationCharacter"
        Animation.AnimationId = "rbxassetid://"..AnimationID
        CharAnimation= Character.Humanoid:LoadAnimation(Animation)
        CharAnimation:Play()
        ContextActionService:BindAction(
            FREEZE_ACTION,
            function()
                return Enum.ContextActionResult.Sink
            end,
            false,
            unpack(Enum.PlayerActions:GetEnumItems())
        )
    end
end

1 answer

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

It's kinda simple really.

Add a LocalScript somewhere (I put mine in StarterCharacterScripts) and put this code:

local uis = game:GetService("UserInputService")

local isCrouching = false -- check if the player is crouching
local isCrawling = false -- check if the player is crawling

local walkingAnimId = 1 -- change this to your walking animation ID
local crouchingAnimId = 1 -- change this to your crouching animation ID
local crawlingAnimId = 1 -- change this to your crawling animation ID

uis.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.C then
        if isCrouching == false and isCrawling == false then
            isCrouching = true
            print("Player is now crouching")
            game.ReplicatedStorage.PlayMovementAnimation:FireServer(crouchingAnimId)
        elseif isCrouching == true then
            isCrouching = false
            isCrawling = true
            print("Player is now crawling")
            game.ReplicatedStorage.PlayMovementAnimation:FireServer(crawlingAnimId)
        elseif isCrawling == true then
            isCrawling = false
            print("Player is now walking")
            game.ReplicatedStorage.PlayMovementAnimation:FireServer(walkingAnimId)
        end
    end
end)

Then, add a RemoteEvent into ReplicatedStorage and call it "PlayMovementAnimation".

And finally, add a ServerScript into ServerScriptService and type this:

local rs = game:GetService("RunService")

local walkAnimId = 1 -- change this to your walking animation ID
local crouchAnimId = 1 -- change this to your crouching animation ID
local crawlAnimId = 1 -- change this to your crawling animation ID

game.ReplicatedStorage.PlayMovementAnimation.OnServerEvent:Connect(function(player, animationId)
    local animation = Instance.new("Animation")
    animation.Parent = player.Character
    animation.AnimationId = animationId

    local playAnimation = player.Character.Humanoid:LoadAnimation(animation)

    if animationId == walkAnimId then
        player.Character.Humanoid.WalkSpeed = 16 -- you can adjust this
    elseif animationId == crouchAnimId then
        player.Character.Humanoid.WalkSpeed = 7 -- you can adjust this
    elseif animationId == crawlAnimId then
        player.Character.Humanoid.WalkSpeed = 4 -- you can adjust this
    end

    while true do
        if player.Character.Humanoid.MoveDirection.Magnitude > 0 then
            playAnimation:Play()
        else
            playAnimation:Stop()
        end
        wait()
    end
end)

Hope this helps. :)

0
Thank you for your response! Though, when I press C nothing happens. Did I do something wrong? The only thing I didn't understand was the ServerScript part. Do you mean by just a script? Preston127313 0 — 3y
0
Yes, I do Scryptol 2 — 3y
0
Well, I put the script in the game, and when I press C once it plays the animation. When I press C again, it plays the 2nd animation, but the 1st one is still playing. It happens again while pressing C a third time. Any fix to this? Preston127313 0 — 3y
0
Oh, yes. When the player presses C, put "playAnimation:Stop()" and then set the animation ID, and after that put "playAnimation:Play()" Scryptol 2 — 3y
Ad

Answer this question