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.


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?

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.

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
Ad

Answer this question