Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

3 comments

0

worked. thanks so much!

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

0

yes u can use a remote event, but this is for simplicity's sake

This discussion is preserved as it was. New answers, comments, and votes are closed.