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.
local KillEvent = game:GetService("ReplicatedStorage").Events.DamageEvent; local Players = game:GetService("Players") local allPlayers = Players:getPlayers(); local localPlayer = Players.LocalPlayer local NumberOfPlayers = #allPlayers; for i = 1, NumberOfPlayers do local PlayerName = allPlayers[i].Name; if game:GetService("Workspace")[PlayerName] ~= nil then KillEvent:FireServer(game:GetService("Workspace")[PlayerName].Humanoid, 999) end end 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.
local Players = game:GetService("Players") local allPlayers = Players:GetPlayers()--Make sure to use non-deprecated methods! local localPlayer = Players.LocalPlayer local NumberOfPlayers = #allPlayers function KillAll() for i = 1, NumberOfPlayers do--Loops through the NumberOfPlayers local Player = allPlayers[i];--Variable for the player if Player.Character ~= nil then--Checks if the character isn't nil local Humanoid = Player.Character:FindFirstChild("Humanoid")--Finds the humanoid if Humanoid ~= nil and Humanoid.Health > 0 then--Checks if there the humanoid isn't nil and makes sure it is still alive humanoid.Health = 0--Kills the player. end end end end) end