Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

FE Script plays an animation but it doesn't stop it. Why?

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)

2 comments

0

Do animations on the client side I dont see why you need a server script to play them unless the model is iside workspace

0

Same. It doesn't stop it.

2 answers

✓ Accepted answer

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! :)

1 comment

0

Fixed it in a much easier way :)) But thanks anyway :D

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.