Hello devs! I am making a game that requires you to hit things with a baseball bat. I am currently making the tool with an animation that plays on activation/click. This is a very simple script and should be working.
local tool = script.Parent
local animation = tool:WaitForChild("Animation")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(animation)
tool.Activated:Connect(function()
if not animationTrack.IsPlaying then
animationTrack:Play()
end
end)
I think it would go like ```local tool = script.Parent
local animation = tool:WaitForChild(“Animation”)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)
local animationController = humanoid:FindFirstChild(“AnimationController”) – it might not be named this but just playtest and look in your humanoid and you should find it
local animationTrack animationController:LoadAnimation(animation)-- again might not be called this funtion
tool.Activated:Connect(function()
if not animationTrack.IsPlaying then
animationTrack:Play()
end
end)```
Sorry if this is confusing i am typing this on my phone
function PlayAnimation(animationData)
local entry = AnimationList[animationData.Name]
if not entry then return end
if ToolEquipped and Humanoid.Health > 0 then
if entry.AnimationTrack then
print("Stopping")
while entry.AnimationTrack.IsPlaying do
entry.AnimationTrack:Stop()
task.wait()
end
print(" stopped")
else
entry.AnimationTrack = Animator:LoadAnimation(entry.Animation)
print("call loading")
while entry.AnimationTrack.Length == 0 do
task.wait()
end
print("loaded, ",entry.AnimationTrack.Length)
end
if animationData.Priority then
entry.AnimationTrack.Priority = animationData.Priority
end
while not entry.AnimationTrack.IsPlaying do
print(".. check play")
entry.AnimationTrack:Play(animationData.FadeTime, animationData.Weight, animationData.Speed)
task.wait(.3)
end
if animationData.StartTime then
entry.AnimationTrack.TimePosition = animationData.StartTime
end
print(" playing!")
end
end
function StopAnimation(animationData)
local entry = AnimationList[animationData.Name]
if not entry then return end
if entry.AnimationTrack then
print("Stopping")
while entry.AnimationTrack.IsPlaying do
entry.AnimationTrack:Stop(animationData.FadeTime)
task.wait()
end
print(" stopped")
end
end
function Equipped(Mouse)
ToolEquipped = true
PlayAnimation({Name = "Idle"})
end
function Unequipped()
ToolEquipped = false
for k,v in pairs(AnimationList) do
StopAnimation({Name=k})
end
end
function Activated()
if not Tool.Enabled or not ToolEquipped or Humanoid.Health <= 0 then
return
end
Tool.Enabled = false
SoundList.Lunge:Play()
PlayAnimation({Name = "Lunge"})
wait(1)
Tool.Enabled = true
end
You are correct, animations on the Server, WILL replicate to all clients, thats why you use them for NPC animtions.
However, for character animations on the Client, they too WILL replicate to all clients, however, it plays on the characters machine first, which makes the animation smooth due to it not having to come from the the server first.
So, if you press the R key, and your character does a gun reloading animation.
If you do it on the server, then…
client detects keypress, and sends message to the server to play animation
server plays animation
animation is replicated back to client. and to all other clients
However, if you play the animation on the client it would be…
client detects keypress and plays animation
animation is replicated to the server
server sends animation to all other clients
The second way you get to see the animation instantly when you press the key.