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.
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.
Add a new RemoteEvent in ReplicatedStorage called “Surrender”:
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)
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)
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.
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.
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.
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
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.
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?