How do i make a keybind function this?

Hello devs! so i have this script that plays an animation as well as a sound and i want to be able to make the sound and animation play when you press a certian key “R” would be the key heres the script

-- Script for playing animation at random times

local model = script.Parent  -- Assuming this script is a child of the model

local animationController = model:FindFirstChildOfClass("AnimationController")

local function playAnimation()
	-- Get a random animation from the animation controller
	local animations = animationController:GetChildren()
	local randomAnimation = script:FindFirstChild("Rawr")

	-- Play the random animation
	animationController:PlayAnimation(randomAnimation)

	-- Wait for a random amount of time before playing the next animation
	local randomWaitTime = math.random(1, 5)  -- Adjust the range as needed
	wait(randomWaitTime)

	-- Repeat the process
	playAnimation()
end

-- Start playing the animation loop
while true do
	playAnimation()
end
animationController:LoadAnimation("randomAnimation"):Play()

the animation is called “Rawr” plz dont ask questions but if anyone can help me that would be great!

1 Like

You can use UserInputService by typing UserInputService.InputBegan:Connect(function(input) into the script. That way we can use KeyCode into the script by typing Enum.KeyCode.R into the input.

Here’s a script:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.R then -- the keycode is r (pressing r)
      animationController:LoadAnimation("randomAnimation"):Play() -- animation will play
    end
end)
2 Likes

which line do i add/remove and replace it with?

It could be any line you want
EDIT: I forgot to mention that UIS doesn’t work in serverscript.

Btw, I would like to recommend you to add LocalScript and adding a RemoteEvent into the ReplicatedStorage.

Just add the script into the LocalScript:

local UIS = game:GetService("UserInputService")
local PlayAnimation = game.ReplicatedStorage.PlayAnimationEvent

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.R then -- the keycode is r (pressing r)
      PlayAnimation:FireServer() -- animation will play
    end
end)

And the ServerScript:

-- Script for playing animation at random times

local model = script.Parent  -- Assuming this script is a child of the model
local PlayAnimationEvent = game.ReplicatedStorage.PlayAnimationEvent  -- the remote event

local animationController = model:FindFirstChildOfClass("AnimationController")

local function playAnimation()
	-- Get a random animation from the animation controller
	local animations = animationController:GetChildren()
	local randomAnimation = script:FindFirstChild("Rawr")

	-- Play the random animation
	animationController:PlayAnimation(randomAnimation)

	-- Wait for a random amount of time before playing the next animation
	local randomWaitTime = math.random(1, 5)  -- Adjust the range as needed
	wait(randomWaitTime)

	-- Repeat the process
	playAnimation()
end

-- Start playing the animation loop
while true do
	playAnimation()
end

PlayAnimationEvent.OnServerEvent:Connect(function()
     animationController:LoadAnimation("randomAnimation"):Play() -- animation will play if fireserver
end)

thats not a server script its a script inside of a skinned mesh

should i make it a serverscript?