Hey, I'm working on a game, and to reset the whole game, I'm just killing everyone. It's the easiest that I could have thought of. So I have this "DamageEvent" that people take damage from.
01 | local KillEvent = game:GetService( "ReplicatedStorage" ).Events.DamageEvent; |
02 | local Players = game:GetService( "Players" ) |
03 | local allPlayers = Players:getPlayers(); |
04 | local localPlayer = Players.LocalPlayer |
05 | local NumberOfPlayers = #allPlayers; |
06 |
07 | for i = 1 , NumberOfPlayers do |
08 | local PlayerName = allPlayers [ i ] .Name; |
09 | if game:GetService( "Workspace" ) [ PlayerName ] ~ = nil then |
10 | KillEvent:FireServer(game:GetService( "Workspace" ) [ PlayerName ] .Humanoid, 999 ) |
11 | end |
12 | end |
13 | end ) |
When some people are not loaded in the game, they are still in the Players Service, but not in the Workspace as Model. So it results as a not Valid Member of Workspace. How can I fix this?
This script will run in a Script not a LocalScript, it is my personal opinion that you should NEVER EVER have damage RemoteEvent's as exploiters can easily use this to loopkill everybody.
01 | local Players = game:GetService( "Players" ) |
02 | local allPlayers = Players:GetPlayers() --Make sure to use non-deprecated methods! |
03 | local localPlayer = Players.LocalPlayer |
04 | local NumberOfPlayers = #allPlayers |
05 |
06 | function KillAll() |
07 | for i = 1 , NumberOfPlayers do --Loops through the NumberOfPlayers |
08 | local Player = allPlayers [ i ] ; --Variable for the player |
09 | if Player.Character ~ = nil then --Checks if the character isn't nil |
10 | local Humanoid = Player.Character:FindFirstChild( "Humanoid" ) --Finds the humanoid |
11 | if Humanoid ~ = nil and Humanoid.Health > 0 then --Checks if there the humanoid isn't nil and makes sure it is still alive |
12 | humanoid.Health = 0 --Kills the player. |
13 | end |
14 | end |
15 | end |
16 | end ) |
17 | end |