Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

6 comments

0

Repeat

0

*Use repeat until to check if the player in the workspace is nil.

0

(Hey, that rhymed!)

0

@GreenGrassGamer2008 how do you mean that? Can you explain haha

0

Don't use LocalScripts to kill players.

0

Repeat wait() until workspace.player ~= nil I guess? It’s not the greatest of ideas but it might work

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.