I have been having this problem for a while now, and I can finally share it. I did use the Roblox Developer Hub for this, and it will still not work. I do not know if I am correctly doing this script right, or not.
Here are some images:
Is this a script or a local script? And where is it under? This all matters as scripts and local scripts can use things the other can’t, and they only run under certain places.
Also please do not post screenshots of code as it can be hard to read to others if you were to use a different color or font, directly pasting the code in a code block
```
```
Make it easier for us to be able to
- read it
- replicate the issue easier; we don’t have to write it all out since it’s an image, we can just copy it
It is a normal script, if you’re talk about what it is under it is in this badge: You visited! - Roblox
Here are a few problems I’ve identified.
-
The code from the first screenshot is written in the context of a LocalScript. Remember to write this as a Script and not use LocalScripts when awarding badges.
-
There was no attempt to identify if the badge was not awarded to the player from the PlayerAdded function.
In the future, I recommend pasting the code as @incapaxx has mentioned so that it is easier for other members to help you.
I’ve made changes to the code that you provided in the second screenshot. Make sure you use a Script.
Replace your code with this and let me know what happens.
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID = 2124459178 -- Change this to your badge ID
local function awardBadge(player, badgeId)
-- check badge can be awarded
if BadgeService:IsLegal(badgeId) and not BadgeService:IsDisabled(badgeId) then
-- award badge
BadgeService:AwardBadge(player.UserId, badgeId)
end
end
local function onPlayerAdded(player)
local hasBadge = false
-- Check if the player has the badge
local success, message = pcall(function()
hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge: " .. tostring(message))
return
end
if hasBadge == true then
-- Assign this player a property/indicator that they own the badge
--
elseif hasBadge == false then
awardBadge(player,badgeID)
end
end
-- Connect 'PlayerAdded' events to the 'onPlayerAdded()' function
Players.PlayerAdded:Connect(onPlayerAdded)
When the old wiki was a thing BadgeService’s api reference said it was server side only and I imagine that still being true.
LocalPlayer
is nil to the server, as the server has no concept of “local player.”
Think of it like this: if you told your teacher to get the local student, wouldn’t they be confused? Who’s the local student?
It works, thank you so much! I will take note on what have you said, so I can remember it in the furture.