How do I make script run through these badges?

So basically I want to make a script that runs through your badges and makes sure if you own it or not, and then if you own that badge it will change the color of a specific part (the part is named the same as the badgeID) from red to green, and if all those parts become green then a specific command will happen (for example a part will be inserted into workspace). How do I make that work?

Batteries are under workspace inside a folder, and the part named as the BadgeID is the part that will change from red to green:
RobloxStudioBeta_ZrieeRL6Fc

The main issue is, some of the badges can be earned in-game, and I want the script to keep checking if the person gets the badge while in-game then the part would still change from red to green for that specific badge, because I noticed if I just make a random If statement, then it will only check once and that’s it, but I want it to keep checking instead.

If it helps, the game is a 1-player server, so if I could make a ServerScript in ServerScriptService I assume it would be better than making LocalScripts.

8 Likes

You can use BadgeService:UserHasBadgeAsync or BadgeService:UserHasBadge to detect if a player has a badge. To check everytime, just do a loop and use the functions listed above.

6 Likes

For exemple, here is a script on how it could work:

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

local badgeId = 0000000 -- change to your badge id

While true do
    task.wait()
	if BadgeService:UserHasBadge(player.UserId, badgeId) then
		print("The user has this badge")
	else
		print("The user does not have this badge")
	end
end

This could be improved on, but this is pretty much the basis of what you want to create.

5 Likes

Do you mind elaborating? How would I make it so that each part of the battery changes to green if the person owns that specific badge?

Apologies I am a horrible scripter

4 Likes

If you want an optimized way to do it, I recommend doing one While loop and check for every badge that is connected to a battery. For exemple:

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

local badge1 = 0000000 -- change to your badge id
local badge2 = 0210319
local badge3 = 1923823

While true do
    task.wait()
	if BadgeService:UserHasBadge(player.UserId, badge1) then
		--change color of battery 1 to green
	else
		--change color of battery 1 to red
	end
    if BadgeService:UserHasBadge(player.UserId, badge2) then
		--change color of battery 2 to green
	else
		--change color of battery 2 to red
	end
    if BadgeService:UserHasBadge(player.UserId, badge3) then
		--change color of battery 3 to green
	else
		--change color of battery 3 to red
	end
end

and so on.
this is probably not the most optimized way to do it, but it should work.

3 Likes

Would there be a way to make it so that I have a table of the badges, example:
RobloxStudioBeta_OIQG3tw95E

And make the if statement (that is inside the loop) check if the user has the badge, then the part inside one of the batteries (which its name is the same badge ID) would change from red to green?

4 Likes

yes, you could place all of the badge id’s in a table. But, I would recommend organizing your table like this:

local Badges = {
   ["BadgeName"] = 0000000 - id of your badge
}

so that you can easily refer to the badge that you want using the name of it
you could refer it like this:

Badges.BadgeName

Overall better idea for organization.

5 Likes

So I added this line in the script:

local Batteries = workspace:WaitForChild("Batteries"):GetDescendants()

I was wondering if I could add in the loop (in the if function to be exact) a way so that it auto detects if the part name == badgeID, then change the part from red to green, how can I do that?

So basically I want to add it to this section of the script:

while true do
	task.wait()
	if BadgeService:UserHasBadge(player.UserId, Badges.Badge1) then
	
	elseif BadgeService:UserHasBadge(player.UserId, Badges.Badge2) then

Also this error shows up in the script:
RobloxStudioBeta_aY6ianEKXf

3 Likes

for the error, you just need to define the player.
if your script is a server script, you will just need to wrap the loop in a PlayerAdded function.

Exemple:

game.Players.PlayerAdded:Connect(function(player)
  -- put function here
end

and it should fix this orange underline.

3 Likes

That seems to have fixed it, thanks!

As for the other question, do you mind letting me know how to work on it?

Again, I apologize for the inconvenience I am a very horrible scripter.

2 Likes

do you mean like changing the part color?

4 Likes

Yes, so like I’d make it:

If player has badge & badgeID == battery-part name then
change battery-part color from red to green

3 Likes

you wouldn’t have to check if the batteryPart.Name is equal to badgeId. But for changing the color, its very simple. Here is the line of code that you would need to implement in your if statement. I also recommend changing the part’s name in the Battery to like “BasePart”. You don’t need it to be the badge’s id.

Battery1.BasePart.Color = Color3.new(0,1,0) -- for changing it to green
Battery1.BasePart.Color = Color3.new(1,0,0) -- for changing it to red
3 Likes

I changed the part inside the Batteries models from the badge ID to BasePart, but for some reason the parts don’t turn green?

Here is the error that shows in output:

Which is line 46:

Here is the current script: (I change badgeID’s here but in the script I used badge ID’s that I do infact own)

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

local ColorGreen = Color3.fromRGB(0, 255, 0)

local Batteries = workspace:WaitForChild("Batteries"):GetChildren()

local Badges = {
	["Badge1"] = 123, -- Badge 1
	["Badge2"] = 1234,  -- Badge 2
	["Badge3"] = 12345-- Badge 3
} -- Place IDs here!

game.Players.PlayerAdded:Connect(function(player)
	while true do
		task.wait()
		if BadgeService:UserHasBadge(player.UserId, Badges.Badge1) then
		Batteries.Battery1.BasePart.Color = ColorGreen
		elseif BadgeService:UserHasBadge(player.UserId, Badges.Badge2) then
		Batteries.Battery2.BasePart.Color = ColorGreen
		elseif BadgeService:UserHasBadge(player.UserId, Badges.Badge3) then
		Batteries.Battery3.BasePart.Color = ColorGreen
		end
	end
end)
2 Likes

change this line to

local Batteries = workspace:WaitForChild("Batteries")

:GetChildren returns a table of the childrens of Batteries and not the folder itself.

2 Likes

For some reason only the first battery part changes color from red to green, while the rest don’t.

Nothing showing in output either :sad:

1 Like

ahh, I just noticed. In your script, it doesn’t check the rest because you used elseif. Elseif will only fire if the first “if” returns false. So you need to make multiple “if statements” for it to work.

2 Likes

Seems to be working, thank you!! (turns out I also forgot to add an “end” at the end of each “if” statement, oops)

One last question if you don’t mind, how can I make it so that it checks through all parts if they change color from red to green, and if they all become green, then lets say another part in workspace would change from yellow to blue? (but I wanted to make it so that it keeps checking if all parts turn green, as some badges can be gotten in-game, so one of the batteries might not be green at the start but turn green after the user got the badge in the same game)

1 Like

This will cause a huge amount of server lag because you’re not supposed to use :UserHasBadge that often.

@Ghostinee

You should have code that first checks if they earned a badge when they joined. Then, every time you award a badge, make sure to set the color to green.

Code:

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

local ColorGreen = Color3.fromRGB(0, 255, 0)

local Batteries = workspace.Batteries

local Badges = {
	Battery1 = 123, -- Badge 1
	Battery2 = 1234,  -- Badge 2
	Battery3 = 12345-- Badge 3
} -- Place IDs here!

Players.PlayerAdded:Connect(function(player)
	for batteryName, badgeId in Badges do
		if BadgeService:UserHasBadgeAsync(player.UserId, badgeId) then
			Batteries[batteryName].BasePart.Color = ColorGreen
		end
	end
end)

Then every time you award a badge:

BadgeService:AwardBadge(userId, badge)
Batteries.WhateverBattery.Color = ColorGreen

If this method is too hard for you, then just use the original script but increase the loop length to avoid overusing BadgeService:

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

local ColorGreen = Color3.fromRGB(0, 255, 0)

local Batteries = workspace.Batteries

local Badges = {
	Battery1 = 123, -- Badge 1
	Battery2 = 1234,  -- Badge 2
	Battery3 = 12345-- Badge 3
} -- Place IDs here!

Players.PlayerAdded:Connect(function(player)
	while task.wait(30) and player:IsDescendantOf(Players) do
		for batteryName, badgeId in Badges do
			local battery = Batteries[batteryName]
			
			if battery.BasePart.Color ~= ColorGreen and BadgeService:UserHasBadgeAsync(player.UserId, badgeId) then
				battery.BasePart.Color = ColorGreen
			end
		end
	end
end)
2 Likes

I thought of using this earlier on, but I thought that would not match his requirements. But now that I think of it, its way better to do it this way.

He would also need to add a PlayerAdded event too so that it saves.

2 Likes