Making a script working on every player

Basically I have a script (the one show below) and it only works if I have a variable with the UserId…How would I make this work for everyone?

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2127369000
local UserId = 1234567

while true do
	wait()
	if BadgeService:UserHasBadgeAsync(UserId, BadgeId) then
		script.Parent.Text = "Paper"
	else
		script.Parent.Text = "Haha"
	end
end
4 Likes

A Player has a UserId property. You can loop through the children (or Players) of the Players service.

1 Like

As I don’t know what type of script you are using I will provide the 2 ways you can get a player instance:

On the client (LocalScript): local player = game.Players.LocalPlayer

On the server (Script): you can get the player instance on join by using the PlayerAdded event which will pass the instance of the player who joined as the callback function’parameter

It was a local script. Thank you so much

What do you mean by that? Sorry, I’m very new to scripting

this sadly didnt work…any reason you can think of why?

When you open a Player object, located under Players in the Explorer, you see a Property named UserId, which is what will be used. Using a for loop and the Players service, we can use Players:GetPlayers() to get a table of players on the session.

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local BadgeId = 124567

while true do
	task.wait(1)
	
	for _, Player in Players:GetPlayers() do -- For each player in Players, do:
		if BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId) then
			-- do whatever
		end
	end
end

Well I understand how this works. It just doesnt work. There is no red lines and no errors. I cant seem to find the problem

Did you mention your code is a LocalScript? The code above works for a normal Script. Try the code in a Script, placed under ServerScriptService.

I just tried that…heres my code

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local BadgeId = 2127369000

while true do
	task.wait(1)

	for _, Player in Players:GetPlayers() do -- For each player in Players, do:
		if BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId) then
			game.StarterGui.ScreenGui.TextButton.Text = "capybara"
		end
	end
end

it doesnt work sadly

6 Likes

You’re changing the StarterGui, the GUI that gets pasted into the Player’s PlayerGui. Use Player.PlayerGui.ScreenGui.TextButton.Text = "capybara".

1 Like

Not sure if this would work, but you can use

local Players = game.Players:GetDescendents

— then in the argument do Players.UserId
1 Like

Whats defining Player? And I didnt even know PlayerGui even existed

Instead of doing while true do then task.wait() just do while task.wait() do

Okay PlayerGui is when you can clone UI into player.PlayerGui and then the UI will only be seen by the player.

This line is under the if BadgeService:UserHasBadgeAsync.... line. Replace this line:

with the one I have provided.

@MRG_GROUPHOLDER There does not seem to be any benefit doing either loop.

I was talking in general.

If it’s a script, try this (I haven’t tested this out yet)

while wait() do
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2127369000
local UserId = 1234567
local players = game.Players:GetChildren()
for I, v in pairs(players) do
	wait()
	if BadgeService:UserHasBadgeAsync(v.UserId, BadgeId) then
		script.Parent.Text = "Paper"
	else
		script.Parent.Text = "Haha"
	end
end

if it’s a local script try this,

while wait() do
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2127369000
local player = game.Players.LocalPlayer
	if BadgeService:UserHasBadgeAsync(player.UserId, BadgeId) then
		script.Parent.Text = "Paper"
	else
		script.Parent.Text = "Haha"
	end
end
1 Like

Try this script I did not add the text because it is from my game.

wait(5)

local badgeID = 2129433737 --Put your badge id here--

local badgeService = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)

wait(1)

if not badgeService:UserHasBadge(player.UserId, badgeID) then

badgeService:AwardBadge(player.UserId, badgeID)

end

end)

Your system does work. I just like having it all in a local script, so im gonna be using @DemonLordShadox `s way of doing it. Thank you so much for all the help though!

1 Like