Hello, I have a problem with the emblems of my game, and it is that in the console all the time this is repeated:
Does anyone know what it could be? The script I use is this one:
local badgeservice = game:GetService("BadgeService")
local id = 2124628997
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
badgeservice:AwardBadge(plr.UserId,id)
end
end)
So with the script you have all you need to do is check is the user has a badge.
local badgeservice = game:GetService("BadgeService")
local id = 2124628997
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
--Check if the player has the badge here
local success, hasBadge = pcall(function()
return badgeservice:UserHasBadgeAsync(plr.UserId, id)
end)
if(hasBadge) then
--If the player has the badge, just return
print(plr.Name.." already has this badge.")
return
else
--If the player does not have the badge yet give them the badge
badgeservice:AwardBadge(plr.UserId,id)
print(plr.Name.." received the badge.")
end
end
end)
Edit: You’re also able to remove the badge from your profile and test the script.
I also added print statements for debugging purposes.
local debounce = false
local badgeservice = game:GetService("BadgeService")
local id = 2124628997
if not debounce then
debounce = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
badgeservice:AwardBadge(plr.UserId,id)
end
end)
debounce = false
end
Yeah, that is normal as you are using a touched event. If touch the part that triggers the function. You can get rid of the print statements as it works as intended.
I told you that you can get rid of the print statements. Those are used for debugging purposes. And in your case the script works as intended not in need of the print statements/debugging.
local badgeservice = game:GetService("BadgeService")
local id = 2124660126
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local success, hasBadge = pcall(function()
return badgeservice:UserHasBadgeAsync(plr.UserId, id)
end)
else
badgeservice:AwardBadge(plr.UserId,id)
print(plr.Name.." received the badge.")
end
end
end)
local badgeservice = game:GetService("BadgeService")
local id = 2124660126
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local success, hasBadge = pcall(function()
return badgeservice:UserHasBadgeAsync(plr.UserId, id)
end)
The above methods are good, but here is something faster.
local badgeservice = game:GetService("BadgeService")
local id = 2124628997
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local Badges = plr:FindFirstChild("Badges") or Instance.new("Folder", plr)
Badges.Name = "Badges"
if not Badges:FindFirstChild(tostring(id)) then
local success, hasBadge = pcall(function()
return badgeservice:UserHasBadgeAsync(plr.UserId, id)
end)
if success and not hasBadge then badgeservice:AwardBadge(plr.UserId,id) end
Instance.new("StringValue", Badges).Name = tostring(id)
end
end
end)
UserHasBadgeAsync will pause the script each time it checks, this method will only check once.