Where to edit the script, to add animation?

  1. What do you want to achieve?
    I got a script I am messing around with and I kinda have difficulties reading it since I took a small break from scripting.

  2. What is the issue?
    I am trying to implement my own animation ID into the script to add the throwing animation.

  3. What solutions have you tried so far?
    I tried adding the animation ID into the empty table but that doesn’t seem to work.

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local Tracks = {}
local InputType = Enum.UserInputType

local BeganConnection, EndedConnection

function playAnimation(animName, ...)
	if Tracks[animName] then
		Tracks[animName]:Play()
	else
		local anim = Tool:FindFirstChild(animName)
		if anim and Tool.Parent and Tool.Parent:FindFirstChild("Humanoid") then
			Tracks[animName] = Tool.Parent.Humanoid:LoadAnimation(anim)
			playAnimation(animName, ...)
		end
	end
end

function stopAnimation(animName)
	if Tracks[animName] then
		Tracks[animName]:Stop()
	end
end

function inputBegan(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftDown", Mouse.Hit.p)
	end
end

function inputEnded(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftUp")
	end
end

function onRemote(func, ...)
	if func == "PlayAnimation" then
		playAnimation(...)
	elseif func == "StopAnimation" then
		stopAnimation(...)
	end
end

function onEquip()
	BeganConnection = UIS.InputBegan:connect(inputBegan)
	EndedConnection = UIS.InputEnded:connect(inputEnded)
end

function onUnequip()
	if BeganConnection then
		BeganConnection:disconnect()
		BeganConnection = nil
	end
	
	if EndedConnection then
		EndedConnection:disconnect()
		EndedConnection = nil
	end
end

Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)
Remote.OnClientEvent:connect(onRemote)
1 Like

From what I can tell,
when input happens on the client it’s sent to the server in inputBegan() and inputEnded().
Then after the server does something with it, it sends a message back which is received in onRemote() which calls playAnimation() with the send through data from the server.
Lastly in playAnimation() it checks Tracks to see if the animation with the given name has been loaded before,
if yes it just plays it.
Otherwise it finds it using Tool:FindFirstChild() and loads it in before playing it.

So if you want to add an animation you have to add it as an child to Tool and make the server script send that animation’s name instead of whatever animation it does now.

2 Likes

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