Hey guys,
I’m working on a script that will play an animation when you press a key in-game. The animations are sort of like idle animations and the character doesn’t really move much. The code is working, but sometimes it lags when I go to use it and some times the animation doesn’t play even though they are loaded. I’ll put the code below, and any help would be greatly appreciated!!
(This function module is just a little module a made as practice for my scripting, but also to make things quicker when I’m writing certain scripts. I’ve included the functions I call just so you can see what’s going on better!)
FunctionModule
local module = {}
function module.NewInstance(InstanceType,Name,Parent)
local New = Instance.new(InstanceType)
New.Name = Name
New.Parent = Parent
return New
end
function module.NewAnimationSetup(Name,Parent,AnimationId)
local NewAnimation = Instance.new("Animation")
NewAnimation.Name = Name
NewAnimation.Parent = Parent
NewAnimation.AnimationId = "http://www.roblox.com/asset/?id="..AnimationId
return NewAnimation
end
AnimationHandler (Server Script in ServerScriptService)
local FunctionModule = require(game.ServerScriptService:WaitForChild("FunctionModule"))
local Animations = {
--Template: {AnimationName = "ToolName",AnimationId = AssetInt64}
{AnimationName = "Idle1",AnimationId = "3567368976"};
{AnimationName = "Idle3",AnimationId = "23456"}
}
local function OnAdded(Player)
Player.CharacterAdded:Connect(function()
local WorkspacePlayer = game:GetService("Workspace"):WaitForChild(Player.Name)
local Animator = WorkspacePlayer.Humanoid:WaitForChild("Animator")
local BostonAnimations = FunctionModule.NewInstance("StringValue","BostonAnimations",Animator)
for i,v in pairs(Animations) do
FunctionModule.NewAnimationSetup(v.AnimationName,BostonAnimations,v.AnimationId)
end
end)
end
game.Players.PlayerAdded:Connect(OnAdded)
AnimationTrigger (LocalScript in StarterGui)
wait(.2)
local RemoteEvent = game.ReplicatedStorage.AnimationEvent
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Debounce = false -- our debounce
local UIS = game:GetService("UserInputService")
local Animations = {
--Template: {AnimationName = "Name",AnimationId = "StringNumber",Key="LetterInCaps",LoadedAnimation = Character:FindFirstChild("Humanoid"):LoadAnimation(game.Workspace[Player.Name].Humanoid.Animator.BostonAnimations.[TheAnimation'sName])}
{AnimationName = "Idle1",AnimationId = "3567368976",Key = "M",LoadedAnimation = Character:FindFirstChild("Humanoid"):LoadAnimation(game.Workspace[Player.Name].Humanoid.Animator.BostonAnimations.Idle1)};
}
UIS.InputBegan:Connect(function(Input,TypeCheck)
if not TypeCheck then -- So that it wont fire when the player is chatting
for i,v in pairs(Animations) do
if Input.KeyCode == Enum.KeyCode[v.Key] and Debounce == false then
Debounce = true
v.LoadedAnimation:Play()
Debounce = false
end
end
end
end)
Thank you for any help you can give!!!