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

How would I have an animation and sound play on keypress?

Asked by 6 years ago

As if a stomp animation was triggered through key which also plays an audio along with it. (Example)

Although, I have the animation part which is non spammable due to a wait value.

3 answers

Log in to vote
0
Answered by 6 years ago

I dont know about key press but you can do like this basic script

keypress thing here
animation.play
wait(0.2)
game.workspace.sound.play()
wait(0.2)
end
0
Well for key press i think this work BluesteelKd 2 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Key presses are tricky, because you need to get them through a LocalScript.

LocalScripts only work if they're stored in the PlayerScripts folder or in a player's Character model. What you have to do is use the game:GetService("UserInputService") in the localscript's code to access keyboard and controller events.

Code: (In a LocalScript)

uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
    if input.KeyCode.Name == "Name of your desired key here" then

        -- code here.
        -- if experimental mode is on, this is where you'd load the animation on game.Players.Localplayer.Character.Humanoid, then play it.
        -- if experimental mode is off (much better, keeps out hackers) then see below.

    end

end)

Read up about UserInputService on the roblox wiki here!

This next part is where things get tricky:

Roblox's new security system means that, unless something called "Experimental Mode" is enabled, LocalScripts can't change anything on the server; if you tell a LocalScript to play an animation when you press a key, only you will be able to see the animation, not any other player in-game. This feature exists so that hackers have less control over your game.

Experimental mode is usually enabled by default, but if you have the time, it's better to get used to working around it as opposed to having to un-learn bad habits later.

If Experimental mode is off, Once you get the key that was pressed in a local script, you have to use a RemoteEvent to tell the server script to play your animation.

I don't have time right now to explain RemoteEvents, but there is a tutorial on the wiki that should help until I can come back and make an edit. LINK IS HERE!

Good luck! Hope this helped!

Log in to vote
0
Answered by 5 years ago

It's not that hard, yet not simple. So, here are the 2 Very Similar Options in doing this LocalScript.

Option 1: Before copying this script, please make sure that the Sound is inside the LocalScript, and make sure this script is in StarterCharacterScripts.

local UIS = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()


local Key = 'E' --Change this to Desired Letter.

UIS.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] then
        print(plr.Name..' has pressed '..Key)
        script.Sound:Play() --Make sure that your Sound is named 'Sound' and is inside of the LocalScript.
    end
end)

Option 2: Before copying this script, please make sure that this Script is in StarterCharacterScript, The Sound is in Workspace, and this is a LocalScript.

local UIS = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()


local Key = 'E' --Change this to Desired Letter.

UIS.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] then
        print(plr.Name..' has pressed '..Key)
        game.Workspace.Sound:Play() --Make sure that your Sound is named 'Sound' and inside the Workspace.
    end
end)

If you still need help with this, Reply to this with your roblox username as I will friend you and there I can continue helping your problem.

Hope this Helped :D

0
GeezyWheezy2006 GeezyWheezy2006 0 — 4y
0
BkBinder20071 BkBinder20071 0 — 3y

Answer this question