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

Trying to make a toggle animation with keybinds?

Asked by 5 years ago
Edited 5 years ago
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')

mouse.KeyDown:Connect(function(Key)
    Key = Key:lower()
    if Key == "c" then
        local Animation = Instance.new("Animation", player.Character)
        Animation.AnimationId = "rbxassetid://3555353330"
        Animate = Humanoid:LoadAnimation(Animation)
Animate:Play()
    end
end)
if mouse.KeyDown:Connect(function(Key)
    Key = Key:lower()
    if Key == "c" then
        Animate:Destroy()
    end
end
end)

It keeps saying "expected ')' (to close '(' at line 15), got 'end'

Any ideas on how I could fix this or just fix the script in general? Its meant to be so every-time you press "c" your character crouches with a custom animation then when you press "c" again it stops the crouching animation and makes you stand like normal. I'm very NEW to scripting so sorry if I look stupid as hell.

edit

After looking at the comments I changed the script to use UserInputService but still have no idea

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')
local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(Input)
    local MouseInput = Input.UserInputType
    local KeyCode = Input.KeyCode

if KeyCode == Enum.KeyCode.C then
    local Animation = Instance.new("Animation", player.Character)
    Animation.AnimationId = "rbxassetid://3555353330"
    Animate = Humanoid:LoadAnimation(Animation)
    Animate:Play()
 end  
end

if KeyCode == Enum.KeyCode.C then
  Animate:Stop()
end
end)
0
dont use keydown use UserInputService The_Pr0fessor 595 — 5y
1
^ AizakkuZ 226 — 5y
0
I changed it to use UserInputService but still don't really know, could any of you help? llMasonIl 12 — 5y
0
lol it'd be cool if people actually helped instead of just correcting deprecated code Psudar 882 — 5y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

Alright I know exactly whats up here.

You're trying to check if an event happens using if-statements.

Fortunately, you don't need to do that. When the event triggers, it will call the corresponding function without having to check it with an if-statement.

Let me show you what i mean

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local debounce = false
UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.C then
        if not debounce then
            debounce = true
            local Animation = Instance.new("Animation")
            Animation.AnimationId = "rbxassetid://3555353330"
            Animation.Parent = player.Character
            Animate = Humanoid:LoadAnimation(Animation)
            Animate:Play()
        else
            Animate:Stop()

            debounce = false
        end
    end  
end)

Okay so you're probably gonna say, "what the heck? this guy just completely rewrote my script", but I'll explain why.

Your script was based off of a false idea that you need to say someting like if event then, so i just rewrote it to fix it up in that regard. I also removed all the deprecated code you were using and replaced it with the recommended versions.

Since you want to toggle the animations, you can use something called a debounce, debounces have many use cases but for this case we're using it as basically an on/off switch for the animation.

Best of luck scripting, here are some sources that may help you out:

--Debounces https://developer.roblox.com/en-us/articles/Debounce --Whats deprecated code? https://roblox.fandom.com/wiki/Deprecation --Deprecated code article https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296

0
Oh and i didnt even address your error code, that error was just saying it expected you to close some parenthesis you missed. Nothing that important but its also fixed here as well. Psudar 882 — 5y
1
Damn, thank you so much! Works perfectly how I wanted it to, I'll make sure to refer back to this script if I ever want to do something that needs a toggle again. (I would upvote but I need 25 rep lmao) llMasonIl 12 — 5y
0
No problem man, goodluck. Glad i could help you out mate. Psudar 882 — 5y
Ad

Answer this question