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
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
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
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.
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.
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
This isn’t working 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)
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.