Hello dear developers.I’m trying to create my own custom badge service to release it to roblox developers.It’s going to be useful for people to create their custom own badges and easy-use.
but the main problem is some functions uses profile service to remove or add data to a profile.Even i did checking if profile is exist,it still gives the error about profile not found.
I have tried looking up on forums having the same problem with me,but i couldn’t find any things about my problem.
(Forgot to tell you guys i’m creating my own plugin.)
Here’s my Badge Service Script. (Server)
local BadgeService = require(workspace:WaitForChild("Badge Service"):WaitForChild("Modules"):WaitForChild("MainModule"))
local Players = game:GetService("Players")
local Part = workspace:WaitForChild("BadgePart")
Part.Touched:Connect(function(Hit)
if Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass("Humanoid") then
local Player = Players:GetPlayerFromCharacter(Hit.Parent)
local Badge = BadgeService:GetBadgeByName("You played!")
print(Badge.Name)
local Id = Badge:GetAttribute("Id")
print(Id)
local OwnsBadge = BadgeService:UserOwnsBadge(Player.UserId,Id)
if not OwnsBadge then
if Badge and Id then
BadgeService:Award(Player.UserId,Id)
end
end
end
end)
BadgeService.OnBadgeAwarded.Event:Connect(function(Player,Id)
print(Player.." has earned badge with given id: "..Id.."!")
end)
and here’s the MainModule of badge service. (Module)
--> Services <--
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
--> Remotes & Modules <--
local Remotes = script.Parent.Parent:WaitForChild("Remotes")
local Modules = script.Parent.Parent:WaitForChild("Modules")
--> Profile Service <--
local PS = require(Modules.ProfileService)
local Template = {}
local ProfileStore = PS.GetProfileStore("BadgeDataStore",Template)
local Profiles = {}
local module = {}
--> Events <--
module.OnBadgeAwarded= Instance.new("BindableEvent")
function module:GetBadgeById(Id)
local SavedBadges = ServerStorage:FindFirstChild("SavedBadges")
if SavedBadges then
local Badge
for _,v in pairs(SavedBadges:GetChildren()) do
if v:GetAttribute("Id") == Id then
Badge = v
end
end
if Badge ~= nil then
return Badge
else
error("BADGE SERVICE | Cannot get the badge with given id. <GetBadgeById>")
end
else
error("BADGE SERVICE | Cannot find 'SavedBadges' folder. <GetBadgeById>")
end
end
function module:GetBadgeByName(Name)
local SavedBadges = ServerStorage:FindFirstChild("SavedBadges")
if SavedBadges then
local Badge = SavedBadges:FindFirstChild(Name)
if Badge ~= nil then
return Badge
else
error("BADGE SERVICE | Cannot get the badge with given name. <GetBadgeByName>")
end
end
end
function module:Award(UserId,Id)
local SavedBadges = ServerStorage:FindFirstChild("SavedBadges")
local Player = Players:GetPlayerByUserId(UserId)
if not Profiles[UserId] then
Profiles[UserId] = ProfileStore:LoadProfileAsync("Player_"..UserId)
end
if Player then
local Badge
for _,v in pairs(SavedBadges:GetChildren()) do
if v:GetAttribute("Id") == Id then
Badge = v:GetAttribute("Id")
end
end
if Badge ~= nil then
local Profile = Profiles[UserId]
if Profile ~= nil then
table.insert(Profile.Data.Badges,Badge:GetAttribute("Id"))
module.OnBadgeAwarded:Fire(Player,Badge:GetAttribute("Id"))
Profiles[UserId]:Save()
end
else
warn("BADGE SERVICE | Cannot award the badge to player. Given Id is invalid or nil. <Award>")
end
else
warn("BADGE SERVICE | Cannot award the badge to player. UserId is invalid or nil. <Award>")
end
end
function module:UnAward(UserId,Id)
local SavedBadges = ServerStorage:FindFirstChild("SavedBadges")
local Player = Players:GetPlayerByUserId(UserId)
if not Profiles[UserId] then
Profiles[UserId] = ProfileStore:LoadProfileAsync("Player_"..UserId)
end
if Player then
local Badge
for _,v in pairs(SavedBadges:GetChildren()) do
if v:GetAttribute("Id") == Id then
Badge = v:GetAttribute("Id")
end
end
if Badge ~= nil then
if Profiles[UserId] then
Profiles[UserId].Data.Badges[Id] = nil
Profiles[UserId]:Save()
else
warn("BADGE SERVICE | No profile has been found. <UnAward>")
end
else
warn("BADGE SERVICE | Cannot UnAward the badge to player. Given Id is invalid or nil. <UnAward>")
end
else
warn("BADGE SERVICE | Cannot UnAward the badge to player. UserId is invalid or nil. <UnAward>")
end
end
function module:GetBadges()
local SavedBadges = ServerStorage:FindFirstChild("SavedBadges")
if SavedBadges then
local Badges = SavedBadges:GetChildren()
if #Badges > 0 then
return Badges
else
warn("BADGE SERVICE | No badges has been found in 'SavedBadges' folder. <GetBadges>")
end
else
warn("BADGE SERVICE | Cannot find 'SavedBadges' folder. <GetBadges>")
end
end
function module:UserOwnsBadge(UserId,Id)
local Player = Players:GetPlayerByUserId(UserId)
if not Profiles[UserId] then
Profiles[UserId] = ProfileStore:LoadProfileAsync("Player_"..UserId)
end
if Profiles[UserId] then
if not Profiles[UserId].Data.Badges then
Profiles[UserId].Data.Badges = {}
end
if Profiles[UserId].Data.Badges[Id] then
return true
else
return false
end
else
warn("BADGE SERVICE | No profile has been found. <UserOwnsBadge>")
return false
end
end
Players.PlayerRemoving:Connect(function(Player)
if Profiles[Player.UserId] then
Profiles[Player.UserId]:Save()
Profiles[Player.UserId]:Release()
end
end)
game:BindToClose(function()
for _,v in ipairs(Players:GetPlayers()) do
if Profiles[v.UserId] then
Profiles[v.UserId]:Save()
Profiles[v.UserId]:Release()
end
end
end)
return module
Here’s the screenshoot of the warning it’s giving
Can anyone help me about how to fix this? I am new at the profile service but i still know it much as possible.
