Local script vs Server script for Tool.Activated

Hello
I am creating a tool, which will increase the player’s health (on the server), when the player activates it.
Is there any benefit to handle the Tool.Activated event in a Local script and fire remote event compared to simply putting a server script handling the Tool.Activated event?

Thank you

Yes, that’s the easiest way to detect a simple click when a tool is equipped.

This should not be done in a localscript.

Example:

local plr = ???

tool.Activated:Connect(function()
  local char = plr.Character
  local hum = char:WaitForChild(“Humanoid”)
  hum.Health += 5
end)
2 Likes

RemoteEvents are inherently insecure so it’d be safer to perform the listening for the tool’s ‘Activated’ signal (event) on the server.

7 Likes

Thank you
I will need also to play an animation when the player uses the tool.
Is it ok to play the animation in the server script? The API reference for the Animator object states that I should play it in a localscript, however if I understand correctly it will still be replicated and played to all other clients (and also on the server?) so I wonder if I am hurting the performance when playing it in a server script?

1 Like

You could have the server script fire a RemoteEvent to the client and then play the animation on the client instead.

And when the client receives the remote event it will play the animation. Then this will be replicated to the server, who will ask the other clients to play the same animation?
In this case is it better the server directly to send the remote event for animation playing to all clients, instead only to the client who activates the tool?
I wonder whether this separation between server/client will bring a performance benefit in this case? May be the benefit will be that the server will not play the animation but only synchronize playing the animation between the clients…

Animations enacted through the character of a client played on that client will replicate to all other clients automatically.

Thank you for your answers.
I just tried the following:

Test 1 - Playing animation using Server script
Local server + player 1 + player2
The server triggers animation for player1.
→ The server sees the animation (ok). Player 1 sees the animation (ok). Player 2 sees the animation(ok).

Test 2 - Playing animation using Local script
Local server + player 1 + player2
Local script triggers animation for player1.
→ Player 1 sees the animation (ok). Player 2 sees the animation (ok). The server sees the animation (I did not expect this…)

It seems that no matter if the animation starts on the server or starts through the Humanoid for player 1 in local script, the animation plays everywhere (including on the server).
So no need to bother with sending remote events from server to player1? We can simply play the animation on the server directly in the Tool.Activated event?

Tell me if my logic is incorrect…

1 Like

I also did the test and it is true. The animation works in a local script and also in a server script. I you notice a difference when using server script, the animation responds faster, which has made me wonder why local script is recommended for animations if they respond better in a server script. If somebody can explain that, thanks