I have a localo scirpt in an image button and normally it works but after I die and click on it slows me and I don’t crouch and I can’t go back to my orig speed
local animationId = "17667816899"
--// Variables
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
-- Reference the CrouchButton
local TextButton = script.Parent -- Directly reference the parent of the script
local crouching = false
local animationTrack
-- Function to Setup Character
local function setupCharacter(character)
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
warn("Animator not found in humanoid! Creating a new one.")
animator = Instance.new("Animator")
animator.Parent = humanoid
end
-- Animation Setup
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. animationId
animationTrack = animator:LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action
-- Reset Crouch State
crouching = false
-- Function to Update Animation Speed
local function updateAnimationSpeed()
if humanoid.MoveDirection.Magnitude > 0 then
animationTrack:AdjustSpeed(1) -- Play at normal speed when moving
else
animationTrack:AdjustSpeed(0) -- Pause animation when idle
end
end
-- Function to Toggle Crouch
local function toggleCrouch()
if crouching then
humanoid.WalkSpeed = 16
crouching = false
animationTrack:Stop()
character.HumanoidRootPart.CanCollide = true
else
humanoid.WalkSpeed = 8
crouching = true
if not animationTrack.IsPlaying then
animationTrack:Play() -- Play the animation only if it's not already playing
end
character.HumanoidRootPart.CanCollide = false
end
-- Update animation speed when crouch state changes
updateAnimationSpeed()
end
-- Connect Text Button (check if button is clicked)
TextButton.Activated:Connect(function()
print("Button Clicked!") -- Debug print to ensure button click works
toggleCrouch() -- Trigger crouch logic
end)
-- Monitor Movement to Update Animation Speed Dynamically
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if crouching then
updateAnimationSpeed()
end
end)
end
-- Setup for Current Character
if player.Character then
setupCharacter(player.Character)
end
-- Listen for Character Respawning
player.CharacterAdded:Connect(function(newCharacter)
setupCharacter(newCharacter)
end)