Help with an Animation Toggled with a keybind

Hello, I am trying to make it so that when a user presses “J” (when not typing ofc) their hands rise in the air in a surrender and all their tools are removed until they un-toggle it by clicking J again.
I have yet to even start on the part that clears their backpack temporarily because thats a whole new can of worms, but the animation toggle script alone isn’t working.



local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local animationId = "rbxassetid://15687505843"
local animation = Instance.new("Animation")
animation.AnimationId = animationId

local animationTrack = humanoid:LoadAnimation(animation)

local key = Enum.KeyCode.J
local isPlaying = false


local function toggleAnimation()
	if isPlaying then
		animationTrack:Stop()
	else
		animationTrack:Play()
	end
	isPlaying = not isPlaying
end


player.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == key then
		toggleAnimation()
	end
end)

Please help, I have consulted several youtube videos with none fixing it.

1 Like

You can’t do player.InputBegan, because that doesn’t exist. Use UserInputService.InputBegan instead.

There’s also another crucial problem with this script. Since it’s a LocalScript, any changes made to the player like clearing the backpack won’t replicate to the server or other players, which is most likely not your intention. To fix this, use RemoteEvents.

  1. Add a new RemoteEvent in ReplicatedStorage called “Surrender”:
    image

  2. Replace your LocalScript with this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Surrender = ReplicatedStorage:WaitForChild("Surrender")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.J then
		Surrender:FireServer()
	end
end)
  1. Add a new Script in ServerScriptService, and paste this inside:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Surrender = ReplicatedStorage:WaitForChild("Surrender")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15687505843"

local surrenderedPlayers = {}
local surrenderAnimations = {}

Surrender.OnServerEvent:Connect(function(player)
	local humanoid = player.Character and player.Character:FindFirstChildWhichIsA("Humanoid")
	
	if not humanoid then
		return
	end
	
	local hasSurrendered = surrenderedPlayers[player]
		
	if hasSurrendered then
		surrenderedPlayers[player] = false
	else
		surrenderedPlayers[player] = true
	end
	
	if surrenderedPlayers[player] then
		local surrenderAnimation = humanoid.Animator:LoadAnimation(animation)
		surrenderAnimation:Play()
		
		surrenderAnimations[player] = surrenderAnimation

		-- Clear backpack (you can try to do it first, ask if you need help)
	else
		surrenderAnimations[player]:Stop()
		
		-- Give items back (same as above comment)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function()
		surrenderedPlayers[player] = nil
		surrenderAnimations[player] = nil
	end)
end)
2 Likes

Still not working when testing. Is it possible for an animation to not be correct? I am a little rusty at animation so I don’t remember exactly. The animation is a single keyframe if thats an issue.

Did you publish the Animation and set it to be Looping?

I definitely published it, let me check if it loops.

this sets it to looping, correct?
image

Where is your LocalScript located? Try putting it under StarterPlayerScript. Meanwhile, you should try printing something inside the lines to determine whether the fault lies on the code or the animation.

It is indeed located in StarterPlayerScript, ill add a few prints

It appears to be be all working, so it is likely the animation’s fault.

Try loading it in the Animator instead of the Humanoid. See if that works. Also check the output if there are errors in your code. If it’s an animation you just published a minute ago, sometimes it takes time for it to load because it gets moderated somehow.

Ahhh, that could be the issue, as I just published it like 10 minutes ago.
Is it fine that the animation priority is set to core?

Could be the issue. I tried your script replaced the animation with mine and the other necessary changes mentioned above and it seems to be working smoothly.
As for your question, not really familiar with animation properties, but core has the lowest priority. Try making it higher so it doesn’t merge with default roblox animations. Could also be the reason.

do you know of any test id that would work so I can confirm if it is my animation?
it might also be a studio beta bug, as scripts often dont work for me

No clue, I’m not an animator.

I don’t. If you have any old animations, you could try that. You could also try changing one digit of the animationId so roblox gives you an error back. If it does give you an error, then it is the animation since that just confirms roblox read your initial animation.

good idea. ill check that real quick

image
Yes, it appears to me my animations fault perhaps then.

may have realized the issue but not 100% sure. So when I am testing it, roblox forces my avatar to be r15, but the animation is r6. how would I force the character to be r6?

Publish the game and configure it manually and set the avatar type to R6.

damn I really thought that would fix it but nope.