Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with UserHasBadge?

Asked by 8 years ago
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!

0
Have you tested it in an actual game? It won't work in studio. Pyrondon 2089 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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)
0
Is their anyway to make this shorter? NinjoOnline 1146 — 8y
0
And even with that it is still not working. I don't see how making a couroutine would affect anything really NinjoOnline 1146 — 8y
Ad

Answer this question