How do i make a keybind function this script?

hello devs! so i have this script that plays a animation at random moments for a skinned mesh but i want to make it so when you click the keybind “R” it plays the animation thats parented to the script and plays a sound along with it heres the script

-- 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 Hum = model:FindFirstChildOfClass("AnimationController")

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

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

	-- Repeat the process
	playAnimation()
end

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

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

can anyone help out?

heres a photo of where its placed and whats inside of it

Screenshot 2023-12-14 183806

1 Like

Use UserInputService and detect, whenever the R keybind (Enum.Keycode.R) has been clicked, then connect your function to it.

1 Like

i have this

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)

its a local script but idk where to put it

1 Like

and currently the script is set to play a random animation i want it to be set to play a animation when you press R

I would just put it inside StarterPlayerScripts for now.

ok ill test it out now

gwgwegwegwffqfqw

2 Likes

it didnt work
gwgwgwgwgwgwgwgwg

Can you use prints to detect where the error is? I believe it is this line:

PlayAnimation:FireServer() -- animation will play

how do i make it print something to tell me errors

Use prints inside your scripts. If it doesnt print, you know that the scripted already errored and stopped

1 Like

yeah i think i need the script remade for me cuz its not printing originally the script was to make it play the animation at a randomwait time so its probably breaking because of that.

Put the localscript in PlayerGui, or StarterPlaye

i got it to work using the roblox assistant

1 Like

Give us an answer how you did it with roblox assisant :sob:

This is how you do what you’re looking to do. Drop that in PlayersGUI or PlayerScripts.
Make sure the server script is right and the remote is in the right place and you got it.

1 Like