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

Is Local all Players with one local possible? [closed]

Asked by 5 years ago
Edited 5 years ago

This is my codes:

local Player = game.Players.PlayerAdded:Wait()
local Character = Player.CharacterAdded:Wait()
print(Player)

But it only print the first player who joined.
I don't want make too much local to get all the player.
Does there any ways to get them easy?
Thanks for helping! By the way, These code is on the npc not player.

Locked by User#24403

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

I think you mean you wanna get all players in one variable, and others are being confused that you wanna get the local player, because of the question title, Avigant answered correct, if you just wanna get the local player use game.Players.LocalPlayer and to get all use game.Players:GetPlayers() but you need more explanation :

LocalPlayer : A property of Player Service, it's an object value, and the value is your player, you maybe wondering There are a lot players, but it's just one object value, the fact is you need to use local scripts to get local player, you can put local scripts in services like StarterGui, Backpack, StartPlayerScripts, StarterCharacterScripts, when player joins game clones local scripts separately for each player, and they run separately, means if you change anything for a player with local script and your game is fe only local player can see it.

Local Variables : I think you know what are variable, and you had understood what does local exactly mean, but here is a another example :

a :

local a = 1
function change()
    local a = 2
end
change()
print(a) -- it would print 1

b :

local a = 1
function change()
    a = 2
end
change()
print(a) -- it would print 2

so that were some more example of local stuffs

and you need table for what you want to do in this question, there are a lot variable types like Boolean, Int, Number, Object, String, Table, EnumValues, Color3, BrickColor and a lot more

Tables : a variable type that holds more than one values, they are also called arrays, a table can hold any variable for example :

local test = {
    String = "abc",
    Object = workspace.Part,
    Number = 123,
    Color = Color3.new(1, 1, 1),
    Bool = true
}

so now if you type print(test.String) it would print abc and if you type print(test.Number) it would print 123 and if you type test.Object:Destroy() it would destroy workspace.Part

so the thing you can do with players that's :

local Players = game.Players:GetPlayers() --It's a table of all players
for _, plr in pairs(Players) do
--This part would run for each players
    print(plr.Name) --it would print there name
    print(plr.Character.HumanoidRootpart.Position) --it would print there character's position
end

I hope i answered the thing you want to know, thanks for reading.

0
Ohhh! This answer is AWESOME!!! Thanks youuuu! MEndermanM 73 — 5y
0
np TheSkyofIndia 150 — 5y
Ad
Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago

Simply use game.Players.LocalPlayer to get the local player. You are confusing local variables with the local player. The local player is simply the client's player instance.

Call game.Players:GetPlayers(), which returns an array of all players.

0
What is GetPlayers() mean? But you answered my questions. Thanks! MEndermanM 73 — 5y
0
Wait. It says localplayer is nil :C btw my script is on the npc. MEndermanM 73 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Nevermind, i found the way can fix this problem.

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
        game:GetService("RunService").Heartbeat:Connect(function()
            if (Player:DistanceFromCharacter(Noob.PrimaryPart.Position)) <= radius and Fighting ~= true then
                print("yay")
            end
        end)
    end)
end)

When npc detect me, It will print yay whatever is which player.