What do you want to achieve? I want to make a crouching script.
What is the issue? When I stop holding down the key, it doesn’t stop the animation.
What solutions have you tried so far? I’ve looked all over Google and the DevForum, and I’ve looked on the Developer Hub.
Here are my scripts:
Server Script in StarterCharacterScripts
local rps = game:GetService("ReplicatedStorage")
local event = rps:WaitForChild("Crouch")
local Moving = false
local Track
event.OnServerEvent:Connect(function(plr, Held)
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.Parent = script
Animation.AnimationId = "rbxassetid://13980069244"
Track = Humanoid:LoadAnimation(Animation)
Track.Looped = true
Track.Priority = Enum.AnimationPriority.Action
Track:AdjustSpeed(0)
if Held == true then
Track:Play()
Humanoid.CameraOffset = Vector3.new(0,-1.2,0)
Humanoid.WalkSpeed = 8
elseif Held == false then
Track:Stop()
Humanoid.CameraOffset = Vector3.new(0,0,0)
Humanoid.WalkSpeed = 16
end
repeat
if Track.IsPlaying == true then
if Humanoid.MoveDirection.Magnitude > 0 then
Track:AdjustSpeed(1)
else
Track:AdjustSpeed(0)
end
end
wait()
until Held == false
end)
Local Script in StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")
local rps = game:GetService("ReplicatedStorage")
local event = rps:WaitForChild("Crouch")
local held = false
local function CheckKeyDown(Key)
if Key.KeyCode == Enum.KeyCode.C then
held = true
event:FireServer(held)
end
end
local function CheckKeyUp(Key)
if Key.KeyCode == Enum.KeyCode.C then
held = false
event:FireServer(held)
end
end
UserInputService.InputBegan:Connect(CheckKeyDown)
UserInputService.InputEnded:Connect(CheckKeyUp)
You’re loading the Track on a single variable inside a server script, meaning even if the server script inside the StarterChracterScripts get cloned to each player who spawns their character, all of that get triggered once even a client tries to play the animation. I highly suggest that you do the animations locally since it will still get replicated to the server so other players will be able to see it.
Here’s a script you can use:
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.Parent = script
Animation.AnimationId = "rbxassetid://13980069244"
Track = Humanoid:LoadAnimation(Animation)
Track.Looped = true
Track.Priority = Enum.AnimationPriority.Action
Track:AdjustSpeed(0)
local function CheckKeyDown(Key)
if Key.KeyCode == Enum.KeyCode.C then
Track:Play()
end
end
local function CheckKeyUp(Key)
if Key.KeyCode == Enum.KeyCode.C then
Track:Stop()
Animation:Destroy()
end
end
UserInputService.InputBegan:Connect(CheckKeyDown)
UserInputService.InputEnded:Connect(CheckKeyUp)
Also it would be better if you would create a ready animation object and then just load it anytime you want to use it instead of creating a new one everytime with Instance.new()
the issue is with the line repeat until Held == false it creates an infinite loop that prevents the animation from stopping when Held becomes false. you need to remove it in order for the animation to stop when Held becomes false.
updated version of the server script:
local rps = game:GetService("ReplicatedStorage")
local event = rps:WaitForChild("Crouch")
event.OnServerEvent:Connect(function(plr, Held)
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://13980069244"
local Track = Humanoid:LoadAnimation(Animation)
Track.Looped = true
Track.Priority = Enum.AnimationPriority.Action
Track:AdjustSpeed(0)
if Held then
Track:Play()
Humanoid.CameraOffset = Vector3.new(0, -1.2, 0)
Humanoid.WalkSpeed = 8
else
Track:Stop()
Humanoid.CameraOffset = Vector3.new(0, 0, 0)
Humanoid.WalkSpeed = 16
end
end)