Why is my random punch animation not working?

Hello. I’m working on a click to punch system. When the player punches, an animation will play. But the animations don’t work.

LocalPunch in StarterPlayerScripts:

local replicatedStorage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local userInputService: UserInputService = game:GetService("UserInputService")

local remoteEvents: Folder = replicatedStorage:FindFirstChild("RemoteEvents") :: Folder

local punchEvent: RemoteEvent = remoteEvents:FindFirstChild("Punch") :: RemoteEvent

local function onInputBegan(input: InputObject, gameProcessedEvent: boolean)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 or gameProcessedEvent then return end
	
	punchEvent:FireServer()
end

userInputService.InputBegan:Connect(onInputBegan)

Server punch script in ServerScriptService:

local players: Players = game:GetService("Players")
local replicatedStorage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage: ServerStorage = game:GetService("ServerStorage")

local punchAnimations: Folder = serverStorage:FindFirstChild("PunchAnimations") :: Folder
local remoteEvents: Folder = replicatedStorage:FindFirstChild("RemoteEvents") :: Folder

local punchEvent: RemoteEvent = remoteEvents:FindFirstChild("Punch") :: RemoteEvent

local function onPlayerAdded(player: Player)
	player:SetAttribute("CanPunch", true)
	player:SetAttribute("IsPunch", false)
end

local function onServerEvent(player: Player)
	local canPunch: boolean = player:GetAttribute("CanPunch")
	local isPunch: boolean = player:GetAttribute("IsPunch")
	
	if not canPunch or isPunch then return end
	
	local character: Model? = player.Character
	local humanoid: Humanoid? = if character then character:FindFirstChildOfClass("Humanoid") else nil
	
	local animator: Animator? = if humanoid then humanoid:FindFirstChildOfClass("Animator") else nil
	if not animator then return end
	
	local animation: Animation = punchAnimations:GetChildren()[math.random(1, #punchAnimations:GetChildren())] :: Animation
	
	local animationTrack: AnimationTrack = animator:LoadAnimation(animation)
	animationTrack:Play()
	
	print(animationTrack)
end

players.PlayerAdded:Connect(onPlayerAdded)
punchEvent.OnServerEvent:Connect(onServerEvent)

No errors in the output, line 32 of the server script prints correctly.

1 Like

If this game is a group game, the animation must be created under the group in order to work

Game is published by my friend. He also owns the animations.

Fixed it by publishing the place to a group.

1 Like

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