local UIS = game:GetService("UserInputService")
local weld = Instance.new("Weld")
local swinging = false
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18263356301"
local function onSwingEvent(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
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 = nil
animTrack:Stop()
swinging = false
end
end
game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)
Make sure some other script isn’t interacting with the animation also.
local UIS = game:GetService("UserInputService")
local weld = Instance.new("Weld")
local swinging = false
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://18263356301"
local animTrack -- Declare animTrack outside the function
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 if it hasn't been loaded yet
if not animTrack then
animTrack = animator:LoadAnimation(animation)
end
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
animTrack:Stop()
weld.Parent = game.ReplicatedStorage
task.wait(0.1)
swinging = false
end
end
game.ReplicatedStorage.SwingEvent.OnServerEvent:Connect(onSwingEvent)