I have a brawler game, and inside of replicated storage, I've got some fake arms that are raycasted and animated. My question is, how do I actually make it so that when I press "e" or press left click, it plays the animations? because with the script I have right now, it doesn't do anything. Here's my code, tell me where I went wrong, and what I can do instead
local player = game.ReplicatedStorage.Arms repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse() local anim = Instance.new("Animation") anim.AnimationId = "rbxassetid//:noseeslol" mouse.KeyDown:connect(function(key) if key == "r" then local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end)
Hello!
There are a few problems with your script. First off, you never parented the animation! You created it, but never gave it a parent.
Second, this script would cause immense lag because every time a player presses "r" it creates a new Animation. Which is just not needed. You can solve your issue by doing this,
local player = game.ReplicatedStorage.Arms local User = game:GetService("Players").LocalPlayer repeat wait() until User.CharacterAdded if User.Character:FindFirstChild("WhateverYouNameTheAnimation") == nil then --Checks if the animation is already there local anim = Instance.new("Animation") anim.AnimationId = "rbxassetid//:noseeslol" anim.Name = "WhateverYouWantToName" anim.Parent = User.Character end local function KeyPress (input, gameProcessed) if input.KeyCode == Enum.KeyCode.R then local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() end end local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(KeyPress)
This should work! If there are any errors then let me know! Happy scripting!
Be sure to upvote if this works and mark as solution!