Introduction
Read through the entire answer instead of just copying the example please.
There are often multiple ways to achieve the same goal, some are more easier and others more effective. If you're accustomed to copying scripts off the internet or some garbage youtube channel stop. In my answer I'll explain how to achieve a goal by breaking the problem down into small chunks, and using official documentation to see how we can fufill each step.
The Roblox Wiki is a great resource, let's use it and break this problem down into simple, manageable steps.
Objective
We want it so that when a player activates a tool (clicks whilst having the tool equipped), all of the players will die.
Steps
1) Invoke a function when the tool is activated.
In Roblox you can "listen" to predefined events which can be found as a member of all instances. Let's check the API for tools and see which event will suit us best.
After scanning through the events section of the webpage you should've found an event called Activated
. This event will "fire" when MouseButton1 (left click) is pressed while the tool is equipped. Lets connect()
an anonymous function to this event, so that our function will be invoked whenever the Activated
event is fired.
2 | local tool = script.Parent |
4 | tool.Activated:connect( function () |
2) Fire a remote event to the server.
Our client will now be able to listen for simple user input, however it won't be able to kill all of the other players since the change will not replicate to the server and therefore to any of the other players. We can use remote events for client-server communication, and kill the players on the server.
Read the API on Remote Events before continuing, it should give you enough information to understand the example below.
2 | local tool = script.Parent |
3 | local toolClicked = game.ReplicatedStorage:WaitForChild( 'ToolClicked' ) |
5 | tool.Activated:connect( function () |
6 | toolClicked:FireServer() |
3) Listen to our remote event on the server.
Once a client fires the remote event ToolClicked
, we can listen for the signal on the server, iterate through all the players and kill each player.
We can connect OnServerEvent
to an anonymous callback function.
2 | local toolClicked = game.ReplicatedStorage:WaitForChild( 'ToolClicked' ) |
4 | toolClicked.OnServerEvent:connect( function (player) |
4) In our callback function we want to iterate or 'loop through' all of the players.
We can retrieve an array of players through the GetPlayers()
function in the Players
service (the documentation for the Players service can be found here). Then we'll iterate through this array of players via a Generic For loop.
1 | toolClicked.OnServerEvent:connect( function (player) |
2 | for i, v in ipairs (game.Players:GetPlayers()) do |
5) Index each player's character and their corresponding humanoid.
Now that we can loop through all of the players lets get their humanoid.
1 | for i, v in ipairs (game.Players:GetPlayers()) do |
2 | local humanoid = v.Character.Humanoid |
6) Kill the character (can be done multiple ways which we will cover below).
Referring back to the statement I made before, there are multiple ways we can achieve a goal, in this case we could've listened for input a different way, perhaps through the InputBegan
event in UserInputService
or maybe even deprecated (outdated and inefficient) methods such as the KeyDown
event (not recommended).
There are also multiple ways to "kill" a character. We can break its joints, remove its head/torso, move its head/torso, call the TakeDamage()
method on a humanoid or simply set its health property to 0. For this example I'll be using the safest method and setting the humanoid's health to 0.
1 | for i, v in ipairs (game.Players:GetPlayers()) do |
2 | local humanoid = v.Character.Humanoid |
Now your scripts should look like this:
Local Script
1 | local tool = script.Parent |
3 | tool.Activated:connect( function () |
4 | toolClicked:FireServer() |
Server Script
1 | toolClicked.OnServerEvent:connect( function (player) |
2 | for i, v in ipairs (game.Players:GetPlayers()) do |
3 | local humanoid = v.Character.Humanoid |
Conclusion
Hopefully you now know how to scan through documentation, find relevant information, break your problem down into small chunks and understand that there isn't "one script for everything". If this answer helped you, accept it; if you liked the answer then upvote it please.