Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
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 Sergiomontani10 236 — 5y
0
Same. It doesn't stop it. bruceywayne 35 — 5y

2 answers

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago

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

0
Fixed it in a much easier way :)) But thanks anyway :D bruceywayne 35 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)

Answer this question