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:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeId = 2143097943

local function onPlayerAdded(player)
    local success, hasBadge = pcall(function()
        return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
    end)

    if not success then
        warn("Error while checking if player has badge!")
        return
    end

    if hasBadge then
        script.Parent.Frame.FortressButton.Text = "FORTRESS"
        script.Parent.Frame.FortressButton.HasBadge.Value = true
    end
end

Players.PlayerAdded:Connect(onPlayerAdded)

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.

local BadgeService = game:GetService("BadgeService")
local badgeId = 2143097943

game.Players.PlayerAdded:Connect(function(plr)
    local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, plr.UserId, badgeId)
    if not success then warn("Your error message") return end

    if hasBadge then
        local Button = plr.PlayerGui:WaitForChild("ScreenGui").Frame.FortressButton
        Button.Text = "YourText"
        Button.HasBadge.Value = true
    end
end)

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