What is "currentplayer"?

Hello, I don’t understand what currentplayer is, or any of the last part of the script for that matter. The devhub keeps introducing new things and its hard to keep up


local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
end

Players.PlayerAdded:Connect(onPlayerAdded)

while true do
	wait(1)
	local playerlist = Players:GetPlayers()
	for currentplayer = 1, #playerlist  do
		local player = playerlist[currentplayer]
		local points = player.leaderstats.Points
		points.Value = points.Value + 1
	end
end

Is currentplayer a variable which has been declared IN the for loop? can you even do that

Simple answer, yes. That’s what is happening here. It’s java equivalent would look like

for (int currentplayer = 0; currentplayer < playerlist.length; currentplayer++){
   var player = playerList[currentplayer];
}
2 Likes

currentplayer is the name given by to whoever wrote that code for the ilerator return of a for loop. In this case, this for loop is an incremental loop going from 1 to the amount of players in the game, used to do a specific thing in this case.

for loops follow this guideline

for (variablename) = (starting number), (ending number) do
    --End
end

You can name the variable anything you want, most people call it i

2 Likes

CurrentPlayer is just the index for where the for loop reached.
I don’t get why people do it this way, possibly because it’s somewhat faster than a for i, v loop, or just beginner’s method.

--this
local player = playerlist[currentplayer]
--is just
local player = playerlist[index] --index for example can be "1" which will represent the first value in the table.
1 Like

by index do you mean like it is cycling through the things

Yes, a for loop goes fro mthe starting number to the ending number, example

for i = 1, 5 do
   print(i) --Prints 1 then 2 then 3 then 4 then 5
end

The varaible just stores the number the loop is currently at

1 Like

but that is exactly the same as a count loop

A count loop? What do you mean? A for loop is already technically a count loop, you can do anything with that number, I only made it just print as an example

1 Like
	if not touched then
		touched = true
		for count = 1, 10 do
			part.Transparency = count / 10
			frame.BackgroundTransparency = count / 10
			wait(0.1)
		end
		part.CanCollide = false
		wait(5)
		part.Transparency = 0
		part.CanCollide = true
		frame.BackgroundTransparency = 0
		touched = false
	end
end

this, line 3

That’s a for loop, count is just the name of the variable, you can name the variable anything you want

2 Likes

count in that situation is a variable name, which can be replaced with anything, just generally it’s descriptive on the situation it’s in.

2 Likes

alright, thank you both but, how can it print 1 then 2 then 3 then 4 then 5, since there is no wait(n) between, wouldnt they all print at once

That’s what I was saying in that example, it’ll print all of them at once since there’s no wait, I wrote it like that to show the order in which they’ll print

1 Like

Yes, technically it’d go through the loop fast enough that it’d loop pretty instant, they’d just be on new lines. the output to his example would legit just be

1
2
3
4
5
1 Like

OH RIGHT, so when it says

for currentplayer = 1, #playerlist do

currentplayer is created on that line and it is counting down from 1 to the #playerlist which has previously been defined, thank you

2 Likes

Yessir, happy we could help you.

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

2 Likes