For i, v in pairs which is the table?

Should I be using ply or _? I forget which one is table?

				for _, ply in pairs(currentPlayers) do
					-- Gets Player table--
					if ply.UserId ~= randomPlayer.UserId then
						local randomPlayer2 = (ply[math.random(1, #ply)])
						randomPlayer2.hiddenstats.Joker.Value = true
					end
				end
2 Likes

ply is the actual player and _ is the index. currentPlayers is the actual table.

Fixed code:

for _, ply in pairs(currentPlayers) do
	-- Gets Player table--
	if ply.UserId ~= randomPlayer.UserId then
		local randomPlayer2 = (currentPlayers[math.random(#ply)])
		randomPlayer2.hiddenstats.Joker.Value = true
	end
end
4 Likes

In the code snippet you provided, both _ and ply are variables. _ is commonly used as a placeholder variable when the value is not needed or used in the loop, whereas ply is used as a variable to store the value of each key-value pair in the currentPlayers table.

Neither _ nor ply has any inherent meaning related to tables. You can use either variable to access table elements, as long as it makes sense in the context of your code. In the example you provided, ply is being used to access the UserId field of each player in the currentPlayers table, so it would make sense to continue using ply to access other fields or methods of the ply variable.

local currentPlayers = game:GetService("Players"):GetPlayers();

for index, value in pairs(currentPlayers) do
    if (value.UserId ~= randomPlayer.UserId) then
         -- do something
    end
end
5 Likes

Oh ok I understand better. A huge help!

1 Like

Thanks!

If my reply helped you out, feel free to mark my reply as the solution! It is greatly appreciated :slight_smile:

1 Like

Pairs is no longer needed.

for index, value in currentPlayers do
    if value.UserId ~= randomPlayer.UserId then
         -- do something
    end
end
1 Like

This won’t/shouldn’t work if currentPlayers is not array-like/(not using the :GetPlayers()), your example tries to iterate over the currentPlayers table directly, which in theory shouldn’t work/is bad practice.

Your example should output
attempt to iterate a table value (field 'currentPlayers')

Hence you aren’t using pairs to iterate over the table value.

It does work. ipairs/pairs are hinted to no longer be used as they maybe deprecated as part of a mission to make luau syntax more better and cleaner.

3 Likes

This is correct, however they are still widely supported and is still being used to-date.

Therefore, it’s still important for devs to understand how the functions work, and when to use them, even if they are eventually deprecated in the future.

May I get help with another simple script?

Why doesn’t this function work? I don’t understand:

local function AwardBadge(player, str)
	if not player or badgeservice:UserHasBadgeAsync(player.UserId, str) then -- Using the "or" operator because by using "and" you're stating, "If the player == nil/false and they have the badge, then don't give them the badge." However, by using "or" you're saying "If the player is nil/false, then don't give them the badge. Furthermore, if the player isn't nil/false, then check if they have the badge and if they still do don't give them the badge!"
		return
	else
		badgeservice:AwardBadge(player.UserId, str) -- If the player satisfies both prequisites, i.e being a player and not having the badge, then give them the badge!
	end	
end

AwardBadge() takes two parameters, the Player (not userID) and the BadgeID

AwardBadge(player : Instance, str: number)

Updating this line should fix your problems.

1 Like

This isn’t working :frowning: idk whats the problem and for some reason and now the first script isnt working. I’ll rejoin to see if that’ll fix it

I was using the other person’s script, I’ll try yours now. I’ve properlly implemented your suggestions

Heres my script for badges rn btw:

local badges = {
	FirstKill = 2141120420;
}

local function AwardBadge(player, str)
	if not player or badgeservice:UserHasBadgeAsync(player, str) then 
		return
	else
		badgeservice:AwardBadge(player, str) 
	end	
end	

findradius.OnServerEvent:Connect(function(player, kniferadius)
	local currentPlayers = Players:GetPlayers()

	for _, ply in pairs(currentPlayers) do
		-- Gets Player table--
		if (player.Character.HumanoidRootPart.Position - ply.Character.HumanoidRootPart.Position).Magnitude < kniferadius and ply.UserId ~= player.UserId then
-- Other stuff that works.--
			AwardBadge(player, badges.FirstKill)
			-- Fires clients and functions--
		end
	end
end)

Where’d you get this information?

I checked the console in game, which I forgot existed. And there is a problem with this:

						local randomPlayer2 = (currentPlayers[math.random(#value)])
						randomPlayer2.hiddenstats.Joker.Value = true

Friend told me about it, turned out to be correct, though it maybe a “shadow change”. Because it’s a recent change that’ll probably covered in the next luau recap.

Does Lua update often, just curious

This was covered a while ago:

cc @weakroblox35

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.