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:
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.
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.
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.
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.
Would there be a way to make it so that I have a table of the badges, example:
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?
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
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
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.
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)
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)
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)