Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

"Player Name" is not a valid Member of Workspace?

Asked by 5 years ago

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.

01local KillEvent = game:GetService("ReplicatedStorage").Events.DamageEvent;
02local Players = game:GetService("Players")
03local allPlayers = Players:getPlayers();
04local localPlayer = Players.LocalPlayer
05local NumberOfPlayers = #allPlayers;
06 
07for 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
13end)

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?

0
Repeat proqrammed 285 — 5y
0
*Use repeat until to check if the player in the workspace is nil. proqrammed 285 — 5y
0
(Hey, that rhymed!) proqrammed 285 — 5y
0
@GreenGrassGamer2008 how do you mean that? Can you explain haha Pap3rLP 6 — 5y
View all comments (2 more)
0
Don't use LocalScripts to kill players. firestarroblox123 440 — 5y
0
Repeat wait() until workspace.player ~= nil I guess? It’s not the greatest of ideas but it might work proqrammed 285 — 5y

1 answer

Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

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.

01local Players = game:GetService("Players")
02local allPlayers = Players:GetPlayers()--Make sure to use non-deprecated methods!
03local localPlayer = Players.LocalPlayer
04local NumberOfPlayers = #allPlayers
05 
06function 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)
17end
Ad

Answer this question