Good Morning. I was making an animation screen system for my game so I thought I’d better check here before putting in game.
The issue is that in the future I may want to add some functions. So I thought of doing something like:
if Animations[v.Name].NeedFunctions and Animations[v.Name].InFunction then
Animations[v.Name].InFunction()
end
But this I already know how to solve. Overall, I wanted to hear from you how I can improve the code itself.
--Services
local UserInputService = game:GetService("UserInputService")
-- Plater
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid : Humanoid = Character:WaitForChild("Humanoid", 100)
--Screengui
local Screengui = script.Parent
local AnimationFolder = Screengui:WaitForChild("Animations")
local ScreenAnimations = Screengui:WaitForChild("AnimationsGUI")
-- Tables
local Animations = {
["Cont"] = {
NeedFunctions = false,
alreadyPlaying = false,
Animation = AnimationFolder:WaitForChild("Salute"),
InFunction = function()
end,
}
}
function searchInside(Animation)
local playingAnimations = Humanoid.Animator:GetPlayingAnimationTracks()
for i, v in next, playingAnimations do
if v.Name == Animation then
v:Stop()
end
end
end
function openClose()
if ScreenAnimations.Visible == true then
ScreenAnimations.Visible = false
for i, v in next, Animations do
searchInside(v.Animation.Name)
end
else
ScreenAnimations.Visible = true
end
end
for i, v in next, ScreenAnimations.Buttons:GetChildren() do
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
if Animations[v.Name] then
if Animations[v.Name].alreadyPlaying == false then
local CurrentAnimation = searchInside(Animations[v.Name].Animation)
local loadAnimation = Humanoid.Animator:LoadAnimation(Animations[v.Name].Animation)
if loadAnimation then
loadAnimation:Play()
Animations[v.Name].alreadyPlaying = true
end
else
searchInside(Animations[v.Name].Animation.Name)
Animations[v.Name].alreadyPlaying = false
end
end
end)
end
end
UserInputService.InputBegan:Connect(function(key, gameProcessed)
if gameProcessed then return end
if key.KeyCode == Enum.KeyCode.M then
openClose()
end
end)
Screengui.TextButton.MouseButton1Click:Connect(function()
openClose()
end)