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

Trying to make a PromoCode that awards a badge when the code is correct not working?

I'm making a game, and I want a player to obtain a badge when they put a code inside a code menu, and when they click the button, the script checks if the text is correct, and if it is, it awards the badge. But its not awarding the badge!

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.TextBox.Text == "placeholder" then
		local Player = game.Players.LocalPlayer
		local badgeid = 2123496874
		local BadgeService = game:GetService("BadgeService")
		if not BadgeService:UserHasBadge(Player.UserId,badgeid) then
			BadgeService:AwardBadge(Player.UserId,badgeid)
			script.Parent.Parent.TextBox.Text = "REDEEMED"
			wait(1.5)
			script.Parent.Parent.TextBox.Text = ""
		end
	end
end)

Whenever I try this in-game, the text changes to REDEEMED, as it should, but doesn't award the badge. Any help?

1 comment

0

The server should be ginig the badge not the player. You also do not want to test on the client side.

1 answer

✓ Accepted answer

###This is because BadgeService is server-side only. You will need remotes to do this. Also, UserHasBadge is deprecated and has been broken by the new game pass update. Switch to UserHasBadgeAsync instead.

###Server code:

local Id = 2123496874
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AwardBadge = ReplicatedStorage:WaitForChild("AwardBadge")
local BadgeService = game:GetService("BadgeService")
local UserHasBadge = ReplicatedStorage:WaitForChild("UserHasBadge")

AwardBadge.OnServerEvent:Connect(function(player)
    BadgeService:AwardBadge(player.UserId, Id)
end)

UserHasBadge.OnServerInvoke = function(player)
    return BadgeService:UserHasBadgeAsync(player.UserId, Id)
end

###Local code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AwardBadge = ReplicatedStorage:WaitForChild("AwardBadge")
local UserHasBadge = ReplicatedStorage:WaitForChild("UserHasBadge")

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Parent.TextBox.Text == "placeholder" then
        local Player = game.Players.LocalPlayer
        
        if not UserHasBadge:InvokeServer() then
            AwardBadge:FireServer()
            script.Parent.Parent.TextBox.Text = "REDEEMED"
            wait(1.5)
            script.Parent.Parent.TextBox.Text = ""
        end
    end
end)

3 comments

0

"The server should be *giving the badge not the player. You also do not want to test on the client side." --kingdom5

1

Wait a sec, in rep. storage, should I make 2 Remote Events? Cause you refer to UserHasBadge in Replicated Storage, and AwardBadge in Rep storage?

0

@Read facepalm the server IS doing that.

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