Hello, So I am currently making a walkspeed potion, but, I need feedback if the code is correct, for example, should I load an animation everytime a tool is activated? and also the it’s a server script so what is the correct way to load an animation in a server script? and I have also head that loading too much animations can lag your game.
Script
local Tool = script.Parent
local Handle = Tool:WaitForChild(“Handle”)
local cooldown = false
Tool.Activated:Connect(function()
if not cooldown then
local animLoad = Tool.Parent:WaitForChild(“Humanoid”):LoadAnimation(Tool:WaitForChild(“Animation”))
If you want a better way to do this I would make a script that handles your animations. I would have a ModuleScript called “AnimationController” or “AnimationHandler” or something along those lines. In the module, you would have all your animations which are loaded to the character whenever the character is added. Now I would have a Get function that returns the animation given, so whenever you want to play an animation you would get the animation from the module and play it. This can be done in ReplicatedStorage so it’s a shared module meaning both the server and client can use it.
If you don’t want to do that you can simply load the animation outside of the activated connection. Hope this helps.
Depends on how many animations you’re trying to load, this is more for animation a player is going to have by default. If it is a tool animation I would leave it in the tool, but let’s say you have emotes, etc, I would recommend having a module.
It’s fine but he is telling you if you want to make an emote system or if you are going to use a lot of animations then it would be better to make a Module for it.
I would personally switch to a local script. The animations will still work. It would do something like this:
--// Services
local Players = game:GetService("Players")
--// Player & Character Instances
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
--// Tool
local Tool = script.Parent
--// Animations
local Animation = Humanoid:LoadAnimation() -- Load animation here
--// Connections
Tool.Activated:Connect(function()
Animation:Play()
end)
Hackers can exploit it though.
He Could probably make a Server and a Local one.
Maybe the Local one for animation and the Server for the Player’s health.
You could always send a remote event that will check if the player has a cooldown, and apply changes. In the remote event, you can check for things like if the player tools are equipped, and are on cooldown. Something like this:
local Cooldowns = {}
RemoteEvent.OnServerEvent:Connect(funcition(player: Player, toolName: string)
-- Check Tool Exist
-- Check if their on cooldown
if player.Character:FindFirstChildWhichIsA("Tool").Name ~= toolName then return end
if Cooldowns[player][toolName] then return end
end)
This maybe a bit annoying if there’s a lot of tools in the game. So instead if you do make a Server script you can check if the animation has been loaded like this:
local AnimationLoaded
Tool.Activated:Connect(function()
if not AnimationLoaded then
AnimationLoaded = Humanoid:LoadAnimation() -- Load Animation Here
end
AnimationLoaded:Play()
end)