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

Script is supposed to check if an object in workspace is a player but it doesn't. Help?

Asked by 6 years ago
if game.Players.NumPlayers < 2 then
        for i, v in pairs(game.Workspace:GetChildren()) do
            if v:FindFirstChild("HumanoidRootPart") then
                if v:WaitForChild("IsLoading").Value == true then
                    status.Value = "There needs to be 1 more player to begin!"
            end
        end
    end
end

The script goes past the first 2 lines but then it gets stuck when it checks for the HumanoidRootPart. It gets get stuck in a way that even if it reads over a player, it doesn't go past the if statement. There is no error and I don't know what to do so I need your help!

0
You could have it search the player list for the name of the player instead. Viking359 161 — 6y
0
I don't understand what and why you do stuff in your code. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

If you want a player's character, do player.Character. If you have a model and want to know if it's a player's character, do game.Players:GetPlayerFromCharacter(model). In your case, it looks like you want to know the number of players that have an IsLoaded boolean value that is set to true.

Notes about your current script:

  • You only run your script if there is only 1 player in the game. Are you sure you didn't want it to be >= 2 on the first line?
  • You should never WaitForChild in a for loop - imagine if IsLoading is never added (ex maybe the player leaves or a script breaks) and WaitForChild never returns? Instead, use FindFirstChild, assigning the result to a local variable.
  • You set status.Value every time you find an IsLoaded value that is true. Is

Recommended script:

function GetNumPlayersLoaded()
    local p = game.Players:GetPlayers()
    local char, isLoaded
    local num = 0
    for i = 1, #p do
        char = p[i].Character
        isLoaded = char and char:FindFirstChild("IsLoaded")
        if isLoaded and isLoaded.Value then
            num = num + 1
        end
    end
    return num
end


if GetNumPlayersLoaded() < 2 then
    status.Value = "There needs to be 1 more player to begin!"
end

Notes about this script:

  • I made the function GetNumPlayersLoaded so that you can use it in multiple places in your script
  • It uses game.Players:GetPlayers() so that it will only get actual players rather than all children that happen to be in game.Players (though usually this is the same thing)
  • If a player doesn't have an IsLoaded value in their character, it is assumed that they are not ready
  • The last 3 lines should be put where-ever you need them; the function should be placed at the top of your script (or at least in a place before you call it)
Ad

Answer this question