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

How would this work using ContextActionService?

Asked by 6 years ago

I'm new to CAService and keyboardInput in general, so how exactly would I go about executing part of a function once a certain key is pressed, (and holding) then when the key is released running the other half? So far I've tried running a portion of it when the UserInputState.Begin (the main animation), then the other portion when UserInputState.End (the idle animation). And for some reason it seems that only the first half runs.

local CAService = game:GetService("ContextActionService")
local deb = false
local function first(actionName, inputState, inputObj)
    if not deb then
        if inputState == Enum.UserInputState.Begin then
            deb = true
            local player = game:GetService("Players").LocalPlayer
            local character = player.Character
            local Animation = Instance.new("Animation")
            Animation.Name = 'Swimming';
            Animation.AnimationId = "rbxassetid://2130790796"
            local track = character.Humanoid:LoadAnimation(Animation)
            track.Looped = true
            track:Play()
            if track.IsPlaying == true and Animation.Name == 'Swimming' then
                character.Humanoid.WalkSpeed = 16
            elseif inputState == Enum.UserInputState.End then
                local Animation = Instance.new("Animation")
                Animation.Name = 'Treading';
                Animation.AnimationId = "rbxassetid://2130697426"
                local track = character.Humanoid:LoadAnimation(Animation)
                track.Looped = true
                track:Play()
                    if track.IsPlaying == true and Animation.Name == 'Treading' then
                    character.Humanoid.WalkSpeed = 0
                    deb = false
                end
            end
        end
    end
end

CAService:BindAction("keyPress", first, false, Enum.KeyCode.Up)

1 answer

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

If you are listening for events such as the beginning of an input and the end of the input separately, I would recommend using UserInputService. This allows you to listen for when when a user presses a key, and comes with useful functions such as IsKeyDown to be able to detect if a user is holding down a key.

Personally, I would use ContextActionService when I want to simply bind an action to a button press, such as binding a punch action on the F key. This way, I will be able to easily allow that mechanism to be available on multiple platforms.

Regarding your script, use the InputBegan event and check if the keycode is Enum.KeyCode.Up, then you can check when the user lets go by constantly checking IsKeyDown(Enum.KeyCode.Up), or using the InputEnded event.

Hope this helps :)

0
Alright man thanks, I'll try that! KardashevScale 110 — 6y
Ad

Answer this question