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

How would i find a player without a localscript & call a function?

Asked by
1ov3y0u 51
4 years ago
Edited 4 years ago

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?

2 answers

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
4 years ago
Edited 4 years ago

Calling 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

LocalPlayer

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!

0
Alright, thanks so much! 1ov3y0u 51 — 4y
0
No problem! starmaq 1290 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)
0
If you need anymore help, I'd be glad to help! ReallyUnikatni 68 — 4y
0
Nice answer! But the player in the player added event is the player *that just joined*! Which is different from the local player that the local script holds, which is the player that has that specific local script starmaq 1290 — 4y
0
Great answer, thanks! 1ov3y0u 51 — 4y
0
Ohh my bad i thought you wanted server side lmao. ReallyUnikatni 68 — 4y

Answer this question