I want to find the player without localplayer but don't know how. How would I define a player without localizing it? Also, how do you call a function?
Wanted to start with this question because it's really simple and different from the other question, or maybe I did not understand what you meant by saying calling a a function. You create a function this way
local function nameOfTheFunction(paramaters) --stuff end
But this on itself won't execute what's inside the function, after defining the function you gotta call
it by writing it's name, two ()
local function nameOfTheFunction(paramaters) --stuff end nameOfTheFunction(paramaters)
Calling a function, is basically telling it to execute. For example, let's say we have a function that calculates the square of something
local function square(num) print(num*num) end square(5) square(2) --you can call a function as many times as you want! that's the main idea of functions: making reusable code
The local player can only be defined from a local script, so doing game.Players.LocalPlayer
in a server script (normal script) will error. Other ways to get the player are many, for example the PlayerAdded
event's which is the player that joined. If you knew the player's name you can do game.Players:FindFirstChild(player's name)
. But the thing with LocalPlayer
is that it's local, here is what I mean: That local script that you put inside of StarterGui, once the game starts, each player (or also called client) gets a copy of that script copied to his PlayerGui
, so there will be a lot of local script, a script for each player! So here is what I mean: for example we have 2 players, player A and player B, player A has a local script to himself and player B has also a local script to himself. If I were to the print(game.Players.LocalPlayer)
in player A's local script, it prints player A! In player B, it prints player B! So the game.Players.LocalPlayer
will be the player that has (runs) that local script! That's why it's special.
So one of the ways to transfer the local player to the server is using remote events. Here is more info!
This can be a little hard to understand when you start scripting.
So one easy way to do this is to have a script get a player when they join.
game.Players.PlayerAdded:Connect(function() end)
PlayerAdded is an event that is fired when a player joins. To get the player that joined you simply do this.
game.Players.PlayerAdded:Connect(function(player) end)
"player" is the player that joined. Now that you can call the player you can do whatever you need with them. For instance, a leader board.
game.Players.PlayerAdded:Connect(function(player) local leaderboard = Instance.new("Folder") leaderboard.Name = "leaderstats" leaderboard.Parent = player --The folders parent (leaderboard) will now be inside the player.) local Money = Instance.new("IntValue") Money.Name = "Money" Money.Parent = leaderboard end)