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
01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03local Animate
04local Humanoid = player.Character:FindFirstChild('Humanoid')
05 
06mouse.KeyDown:Connect(function(Key)
07    Key = Key:lower()
08    if Key == "c" then
09        local Animation = Instance.new("Animation", player.Character)
10        Animation.AnimationId = "rbxassetid://3555353330"
11        Animate = Humanoid:LoadAnimation(Animation)
12Animate:Play()
13    end
14end)
15if mouse.KeyDown:Connect(function(Key)
View all 21 lines...

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

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03local Animate
04local Humanoid = player.Character:FindFirstChild('Humanoid')
05local UIS = game:GetService("UserInputService")
06 
07UIS.InputBegan:connect(function(Input)
08    local MouseInput = Input.UserInputType
09    local KeyCode = Input.KeyCode
10 
11if KeyCode == Enum.KeyCode.C then
12    local Animation = Instance.new("Animation", player.Character)
13    Animation.AnimationId = "rbxassetid://3555353330"
14    Animate = Humanoid:LoadAnimation(Animation)
15    Animate:Play()
View all 22 lines...
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

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03local Character = player.Character or player.CharacterAdded:Wait()
04local Humanoid = Character:WaitForChild("Humanoid")
05local UIS = game:GetService("UserInputService")
06 
07local debounce = false
08UIS.InputBegan:Connect(function(Input)
09    if Input.KeyCode == Enum.KeyCode.C then
10        if not debounce then
11            debounce = true
12            local Animation = Instance.new("Animation")
13            Animation.AnimationId = "rbxassetid://3555353330"
14            Animation.Parent = player.Character
15            Animate = Humanoid:LoadAnimation(Animation)
View all 23 lines...

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