Swing Function Not Working?

Everything in my script works EXCEPT for one thing, the animation.

The animation plays but my problem is, it won’t stop playing after I tell it to.

–Here’s The Script

local UIS = game:GetService("UserInputService")

local weld = Instance.new("Weld")

local swinging = false

game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(function (plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	local Animator = hum:WaitForChild("Animator")
	local animation = Instance.new("Animation")

	animation.AnimationId = "rbxassetid://18263356301"

	local animTrack = Animator:LoadAnimation(animation)



	if swinging == false then
		char.PrimaryPart.CFrame = game.Workspace.Cylinder.CFrame * CFrame.new(0,10,0)
		weld.Part0 = game.Workspace.Cylinder
		weld.Part1 = char.PrimaryPart
		weld.Parent = char
		weld.C1 = CFrame.new(0,2,0)
		animTrack:Play()
		swinging = true
	else
		weld.Parent = game.ReplicatedStorage
		task.wait(0.1)
		animTrack:Stop()
		swinging = false
	end
end)

You should load the animation only once and store the animation track.

local UIS = game:GetService("UserInputService")

local weld = Instance.new("Weld")
local swinging = false

local function onSwingEvent(plr)
    local char = plr.Character or plr.CharacterAdded:Wait()
    local hum = char:WaitForChild("Humanoid")
    local animator = hum:WaitForChild("Animator")

    -- Load the animation only once
    local animation = game.ReplicatedStorage:FindFirstChild("SwingAnimation")
    if not animation then
        animation = Instance.new("Animation")
        animation.AnimationId = "rbxassetid://18263356301"
        animation.Name = "SwingAnimation"
        animation.Parent = game.ReplicatedStorage
    end

    local animTrack = animator:LoadAnimation(animation)

    if not swinging then
        char.PrimaryPart.CFrame = game.Workspace.Cylinder.CFrame * CFrame.new(0, 10, 0)
        weld.Part0 = game.Workspace.Cylinder
        weld.Part1 = char.PrimaryPart
        weld.Parent = char
        weld.C1 = CFrame.new(0, 2, 0)
        animTrack:Play()
        swinging = true
    else
        weld.Parent = game.ReplicatedStorage
        task.wait(0.1)
        animTrack:Stop()
        swinging = false
    end
end

game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)

thats probably a lot smarter then what i was doing but the animation track still wont stop?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.