Hello, I am trying to make a tool in my game that kills everyone in the server and I have tried writing a script for that for a while now but i can't make it work.
tool.Activated:Connect(function() game.Players:BreakJoints() end)
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.
-- Our local script is a child of the tool. local tool = script.Parent tool.Activated:connect(function() -- stuff end)
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.
-- Our local script is a child of the tool. local tool = script.Parent local toolClicked = game.ReplicatedStorage:WaitForChild('ToolClicked') -- the remote event 'ToolClicked' is placed in ReplicatedStorage, which is available to both the server and client. tool.Activated:connect(function() toolClicked:FireServer() -- The player is automatically passed as the first argument. end)
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.
-- In a server script (preferably in the tool or server script service) local toolClicked = game.ReplicatedStorage:WaitForChild('ToolClicked') toolClicked.OnServerEvent:connect(function(player) -- stuff end)
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.
toolClicked.OnServerEvent:connect(function(player) for i, v in ipairs(game.Players:GetPlayers()) do end end)
5) Index each player's character and their corresponding humanoid.
Now that we can loop through all of the players lets get their humanoid.
for i, v in ipairs(game.Players:GetPlayers()) do local humanoid = v.Character.Humanoid end
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.
for i, v in ipairs(game.Players:GetPlayers()) do local humanoid = v.Character.Humanoid humanoid.Health = 0 end
Now your scripts should look like this:
Local Script
local tool = script.Parent tool.Activated:connect(function() toolClicked:FireServer() end)
Server Script
toolClicked.OnServerEvent:connect(function(player) for i, v in ipairs(game.Players:GetPlayers()) do local humanoid = v.Character.Humanoid humanoid.Health = 0 end end)
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.
Make a loop inside the function to go through all the players and kill them