Looping Through Players - Code I Use All The Time Breaking For Some Reason

Hi all,

Very bizarre issue. I’ve had no problem with the coding in a game I’ve made, but suddenly, most likely after a Roblox update, there’s a bit of code that’s triggering a strange error. The strangest thing is that this code is used in other parts of the script, but it doesn’t break it.

The code is for looping through players in a for loop.

        for _,each in pairs(game.Players:GetPlayers()) do
            if each then
				if each.Character then
				    if each.Character.Humanoid.WalkSpeed ~= 16 then
					    each.Character.Humanoid.WalkSpeed = 16
				    end
			    end
			end
		end

The error the game gives is;

“Error in Script on Line 1140: attempt to call a number value”. To clarify, “Line 1140” is the “for _,each in pairs(game.Players:GetPlayers()) do”. I use the exact line for looping through the players before this line in the game and it doesn’t cause a problem. Plus, for 3 years this game has used the same scripting without any problems. Only within the last week or two has it caused an issue.

EDIT: If I comment out that code, it makes the previous for loop that loops through the players have the same issue even if it wasn’t a problem before.

If anyone has any ideas or suggestions, please let me know.

Thanks,
Ryan.

3 Likes

There’s nothing wrong in the code provided. Can we see the rest of the script; more importantly the few lines before this?

1 Like

We need more code than this. There’s nothing wrong with the code you specified, but if I had to take a guess, you override ‘pairs’ with another variable. I’d try searching for “pairs” and seeing if you redefined it anywhere. (Ctrl+F)

4 Likes

With this loop, I don’t see much wrong with it. However, as it seems like the script it’s stored in is quite lengthy, it’s possible that ‘each’ is being used elsewhere in the script. If so, the for loop may not function right, as it’d take the one used prior. (This is only an assumption, though). In the meantime, if it continues to fail, wrapping in pcall could help.

PS I do enjoy your auto games, so hopefully this is patched up soon! :grinning:

2 Likes

I mean, here are 1139 lines of code before this one…which is a lot. I also mentioned that I have not touched this code for ages and it was working perfectly fine until recently.

I don’t really want to post all the code for the game as I don’t want it being leaked in any way, but if that helps work out what’s going on then I can.

It has really puzzled me!

Maybe change the :GetPlayers() with a :GetChildren() and see if that works.

Looks like an error on roblox’s end. Can you try testing this in an actual game? If that doesn’t work, you can manually get players like this

        for _,each in pairs(game.Players:GetChildren()) do
	               if each:IsA("Player") then
                       if each then
	          			if each.Character then
			           	    if each.Character.Humanoid.WalkSpeed ~= 16 then
			           		    each.Character.Humanoid.WalkSpeed = 16
				               end
			               end
			           end
		           end
               end

Try placing a breakpoint/print on that line and see if pairs hasn’t been overriden with something else.

1 Like

Unlike what others are saying, it’s nothing to do with :GetPlayers(); that is a built in function that returns players, as expected.

We don’t need the whole script, just anything before line 1140. From the error, it’s suggesting that you “called” a number value as if it were a function. In that case, the only function being called on line 1140 is the pairs() iterator. Therefore, before line 1140, you may have overwritten “pairs” as a variable.

Try searching through the script (probably easier using CTRL+F) to find any use of “pairs” throughout the script and see if you declare it as a variable elsewhere.

2 Likes

I’m quite certain I only use “pairs” in the for loops, however, as the game does have teams of 2, it might be possible I’ve accidentally used pairs at some point.

Just very strange that it works fine for over a year and suddenly it doesn’t now.

This is an invalid change. GetPlayers is a version of GetChildren that only runs for Player-class instances. If GetPlayers doesn’t work, GetChildren won’t either. GetChildren is also not quite the proper function to run for collecting players either.

cc @azahid1

1 Like

After some tinkering with the code in Studio, it seems there are 2 possible ways how the error could’ve popped up.

  • You may have used pairs as a variable some point in the script.
  • You had defined game as something else in the script.
    What I’d do to be safe is use Ctrl + H and type in the first row “pairs =” (or “pairs=”). If anything appears, that could be what’s causing it. Change the variable names for that, and hopefully other things should be running smoothly. (do the same thing for game too, just in case!)

Every “pairs” in the script is part of a for loop.

EDIT: I updated the variable “each” to “each2” and that did not affect anything. It still broke with the same error.

EDIT 2: Tried reverting the game to a definitely working version from January and that also broke with the same issue.

This error would make more sense if it were the line where you check the character’s WalkSpeed. In that statement, you are making lots of assumptions about there always being a Character and Humanoid inside of it at all times, for all players. Since the error references a number value, it may be related to comparing a number to nil. You should try something as so:

local hum = each.Character and each.Character:FindFirstChild(“Humanoid”)
if hum and hum.WalkSpeed ~= 16 then
     hum.WalkSpeed = 16
end
1 Like

Honestly don’t know why you’re looping and only checking values, which isn’t good. I also am lead to believe you’re using this to limit it specifically to 16, which if a client exploits it the server won’t know it’s changed.
You should be listening to a property change.

for i,v in next, game:GetService('Players'):GetPlayers() do
 local char = v.Character or v.CharacterAdded:wait()
 local hum = char:FindFirstChild('Humanoid')
 hum:GetPropertyChangedSignal('WalkSpeed'):Connect(function()
  hum.WalkSpeed = 16
 end)

It’d be better if you have this inside the PlayerAdded function or as a separate function, and then signal all players to it to listen on CharacterAdded.

It’s not to limit it to 16. The game changes the player’s speed between 0 and 16 depending on what part of the game is happening. The game puts users at a speed of 0 when they are not allowed to move then makes their speed 16 when they are allowed to move again.

Ah? Alrighty, then this code should work.

local function SetWalkSpeed(num)
	for i,v in next, game:GetService('Players'):GetPlayers() do
		if v.Character then
			local hum = v.Character:FindFirstChild('Humanoid')
			if hum then
				hum.WalkSpeed = num
			end
		end
	end
end
SetWalkSpeed(16)