Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help with UserHasBadge?

local replicatedstorage = game:GetService("ReplicatedStorage")
local badgeservice = game:GetService("BadgeService")

local badges = replicatedstorage:WaitForChild("Badges")

local alphaid = badges:WaitForChild("Alpha")

game.Players.PlayerAdded:connect(function(player)
	if badgeservice:UserHasBadge(player.userId, alphaid.Value) then
		print("1")
		player.PlayerItems.Alpha.Value = true
	end
	print("Done")
end)

I have the badge, but when I join it doesn't work. The only other thing in the script is just created an Instance of PlayerItems and the Alpha, but it prints "Done" so it should work, it's just not picking up that I have the badge. Should I add a wait or something?. I have checked the wiki and it should work.

alphaid is an IntValue

Please help!

1 comment

0

Have you tested it in an actual game? It won't work in studio.

1 answer

You're using WaitForChild before you're listening for PlayerAdded, so the player is joining before the objects have loaded into the game. Here's how you can fix it.

local replicatedstorage = game:GetService("ReplicatedStorage")
local badgeservice = game:GetService("BadgeService")

local badges = replicatedstorage:WaitForChild("Badges")

local alphaid = badges:WaitForChild("Alpha")

local function badges(player)
    if badgeservice:UserHasBadge(player.userId, alphaid.Value) then
        print("1")
        player.PlayerItems.Alpha.Value = true
    end
    print("Done")
end

for i, v in pairs(game:GetService("Players"):GetPlayers()) do
coroutine.resume(coroutine.create(badges),v)
end

game:GetService("Players").PlayerAdded:connect(badges)

2 comments

0

Is their anyway to make this shorter?

0

And even with that it is still not working. I don't see how making a couroutine would affect anything really

This discussion is preserved as it was. New answers, comments, and votes are closed.