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

LocalScript that checks if a player has this badge doesn't work? [CLOSED]

Asked by 1 year ago
Edited 1 year ago

im making a local script that checks if a player has this badge, it does something to a text button if so

this, is the script:

01local BadgeService = game:GetService("BadgeService")
02local Players = game:GetService("Players")
03 
04local badgeId = 2143097943
05 
06local function onPlayerAdded(player)
07    local success, hasBadge = pcall(function()
08        return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
09    end)
10 
11    if not success then
12        warn("Error while checking if player has badge!")
13        return
14    end
15 
View all 22 lines...

this local script is inside a screengui, and it doesn't work. there is no errors in the output, i tried looking for solutions but cant find one.

any help is appreciated!

1 answer

Log in to vote
0
Answered by 1 year ago

You shouldn't use a LocalScript when dealing with a PlayerAdded function.

Here's a simple fix.

01local BadgeService = game:GetService("BadgeService")
02local badgeId = 2143097943
03 
04game.Players.PlayerAdded:Connect(function(plr)
05    local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, plr.UserId, badgeId)
06    if not success then warn("Your error message") return end
07 
08    if hasBadge then
09        local Button = plr.PlayerGui:WaitForChild("ScreenGui").Frame.FortressButton
10        Button.Text = "YourText"
11        Button.HasBadge.Value = true
12    end
13end)

This is a server script in ServerScriptService.

0
worked. thanks so much! krLuCiEzkr 22 — 1y
0
LocalScripts can use Players.PlayerAdded, just not to pick up on the local player's connection. This is because the client is already connected to the server, as they had to download the LocalScript to begin with. It's considered a bad practice to manipulate a client's UI on the server, so the proper solution would be to remove Players.PlayerAdded from the LocalScript instead of moving to a Script Ziffixture 6913 — 1y
0
yes u can use a remote event, but this is for simplicity's sake MaleficentMaestro 109 — 1y
Ad

Answer this question