Hey. I want to do a Crouch System that when I press C it plays an Animation and when I press it again it stops playing the animation. It does play the animation but for some reason it doesn't stop it. No errors appear in the Output. The system is Filtering Enabled.
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local crouchOnEvent = Instance.new("RemoteEvent", ReplicatedStorage) crouchOnEvent.Name = "CrouchOnEvent" local crouchOffEvent = Instance.new("RemoteEvent", ReplicatedStorage) crouchOffEvent.Name = "CrouchOffEvent" local animation = Instance.new("Animation") animation.AnimationId = "http://roblox.com/asset/?id=1276087541" local function onCrouchOnFired(plr) local char = game.Workspace:FindFirstChild(plr.Name) local humanoid = char.Humanoid animTrack = humanoid:LoadAnimation(animation) animTrack:Play() end crouchOnEvent.OnServerEvent:Connect(onCrouchOnFired) local function onCrouchOffFired(plr) local char = game.Workspace:FindFirstChild(plr.Name) local humanoid = char.Humanoid animTrack = humanoid:LoadAnimation(animation) animTrack:Stop() end crouchOffEvent.OnServerEvent:Connect(onCrouchOffFired)
Local Script:
local UserInputService = game:GetService("UserInputService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local crouchOnEvent = ReplicatedStorage:WaitForChild("CrouchOnEvent") local crouchOffEvent = ReplicatedStorage:WaitForChild("CrouchOffEvent") crouch = false local function crouchOn(inputObject, gameProcessed) if inputObject.KeyCode == Enum.KeyCode.C and crouch == false then crouchOnEvent:FireServer() crouch = true end end UserInputService.InputBegan:Connect(crouchOn) local function crouchOff(inputObject, gameProcessed) if inputObject.KeyCode == Enum.KeyCode.C and crouch == true then crouchOffEvent:FireServer() crouch = false end end UserInputService.InputBegan:Connect(crouchOff)
Instead of loading an animation then trying to Stop()
it, try using GetPlayingAnimationTracks and stop the animation by looping through the array that it returns.
Hope this helps! :)
Fixed it. This is how:
local UserInputService = game:GetService("UserInputService") local character = game.Players.LocalPlayer.Character local humanoid = character.Humanoid animation = Instance.new("Animation") animation.AnimationId = "http://roblox.com/asset/?id=1276087541" animTrack = humanoid:LoadAnimation(animation) UserInputService.InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.C then animTrack:Play() end end) UserInputService.InputEnded:Connect(function(key) if key.KeyCode == Enum.KeyCode.C then animTrack:Stop() end end)