Question for a tool

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”))

	cooldown = true
	animLoad:Play()
	Handle.Drink:Play()
	Tool.Parent.Humanoid.WalkSpeed = 21
	wait(7)
	Tool.Parent.Humanoid.WalkSpeed = 16
	wait(3)
	cooldown = false
end

end)

Sorry if the code looks messy, I am just new to posting

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.

But how? it’s a script no local i want other players to see the playing animation

They will still be able to see it, I believe with the way roblox handles animations.

won’t I reach the maximum animation loading limit?

Looks good, But add

task.wait()

Not

wait()
1 Like

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.

Yeah, but I don’t think he needs a lot of animations though.

confusing, So my code looks fine???

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.

Yeah your code looks fine, though I would load the animation outside of the activated connection so your not constantly loading animations.

1 Like

wow, Thanks for the help dude!

1 Like

No problem! I also would recommend switch your wait() to task.wait() like @lilmazen1234 said.

but what do you mean by loading the animation outside of the event? it’s a server script is there any other way than using a module script?

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)
1 Like

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)

I will use that for a health potion which I will make soon