Role script not working properly

Hey, I’m making a script that gives you a role depending on your team.
Currently there’s 3 teams (New Yeller, Really red and Navy blue). New Yeller and Really red teams work, and get their role given.
However when it comes to Navy blue, it doesn’t work properly. The code for the role given to the Navy blue team members checks if there’s anyone with the role “SCP-966” and if there is, rolls a number between 1 and 3, if it’s the number 2, then it returns “See No Evil”. Else, it returns “MTF”.

if team == BrickColor.new("Navy blue") then --checks if team is Navy blue
			for i,v in pairs (game:GetService("Players"):GetChildren()) do --gets the players
				if v:FindFirstChild("Role").Value == "SCP-966" then --if a player has the role "SCP-966"
					local number = math.random(1,3) --rolls number between 1 and 3
					if number == 2 then 
					return("See No Evil") --Returns "See No Evil"
					end
				else
						return("MTF") --else it just returns "MTF"
						end
						end
						end

The problem is: I go to test with 2 players, one player spawns in the Really red team and gets “SCP-966” role, the other spawns in the Navy blue team and always gets “MTF” role. Any help would really be appreciated.

1 Like

Does your script even get to number part? And if number gets to be 1 or 3 should it return “MTF”?

I guess the problem with the code is above the random number choosing part, as for the random number checking part, I tried it myself and it worked.

Didn’t check if it does get to the number part. If the number is something other than “2” then it returns “MTF”.

Found out the issue. Try adding a wait() before checking the if the number is 2.

If you want it to return “MTF” if the number isn’t 2 then add another else return “MTF” between return “See No Evil” and end because there isn’t already an else statement for that.

I’ll try it out. Testing with 2 players on ROBLOX studio really slows down my computer.

I added two print checks, one before the “if v:FindFirstChild(“Role”).Value…” part, and one after it. The print check before it gets printed, but the one after it doesn’t. So I suppose the problem is in the role check part of the code.

2 Likes

Maybe it’s the problem with role value? Is there any other part of the code that includes role value that you think could be useful?

This is the code that gives a role to the players in the Really red team:

if team == BrickColor.new("Really red") then
			for i,v in pairs (game:GetService("Players"):GetChildren()) do
				local number = math.random(1,3)
				if number == 1 then
					rep:FindFirstChild("106").Script:Clone().Parent = player.Backpack
								rep:FindFirstChild("106").Arm:Clone().Parent = player.Backpack
					return("SCP-106")
					
				end
				if number == 2 then
					rep:FindFirstChild("457").Script:Clone().Parent = player.Backpack
								rep:FindFirstChild("457").Arm:Clone().Parent = player.Backpack
					return("SCP-457")
					
				end
				if number == 3 then
					return("SCP-966")
			end
			end
			end

edit: changed SCP team to Really red team

There’s no problem with this part of code though. Whenever a character spawns and their role is given, there’s a GUI telling them their role, and it displays the correct role. I even check in Explorer and it is correct.

Is role child of the player? Because you did v:FindFirstChild("Role’')?

Did you check above the numbers like for any errors? or you can turn it into MTF as well to make it better.

Yes, Role is a StringValue, and the player is its parent.

No, there are no errors given in the output. It ignores the role check part and skips straight to giving the “MTF” role.

Do player that is in Navy blue team gets SCP - 966 role before the check if player has that role?

Err, no. Players in the Navy blue team don’t get the SCP-966 role. Players in the Really red team do, though.

The code checks if there is anybody with the SCP-966 role (so a player in the Really red team could have the SCP-966 role). If there is, then it would roll a number between 1 and 3. And if the number is 2, then they would get the “See No Evil” role.

Edit: I think I could have misunderstood what you meant. The player in the Navy blue team spawns after the player with in the SCP team gets the SCP-966 role.

Uh I think I maybe found it.
You want to get all the players and loop all of them until you get one player with SCP - 966 role and then do the rest of the code. The problem is you check if first player has the role and if he doesn’t then it returns MTF. So it doesn’t loop all the players, instead if player 1 doesn’t have the role, script just returns MTF instead of checking next player. Just remove the last else return “MTF” check and the script should work.

But doesn’t i, v in pairs (game:GetService(“Players”):GetChildren()) do get all the players?

Doesn’t return end the function? It does get all the players but doesn’t check all of them because the function ends after checking first player.