[SOLVED] SurfaceGui that Shows Random Sentences, Certain Sentence Awards Badge

The official documentation disagrees. math the square brackets mean it includes that number. I can’t test right now but I’ve never encountered that in practice.

You will need to change this to a server script, so just a little change. If you need help, let me know!

https://developer.roblox.com/en-us/api-reference/function/BadgeService/AwardBadge

The badge still doesn’t award, thanks for helping though

I need a lot of help… as I said I barely just started scripting lol. How would I get it to work?

The way this should be done is there should be a button.

That button should fire a remote event to let the server know it’s been pressed. The server will then pick the string award the badge and send the string back through the remote event to let the client know what the button should now say.

Sure they could fire the remote event whenever they want, but the server the one who verifies its validity. If the button can’t always be pressed the server should have sanity checks that ensure the button could have been pressed before it does anything.

As long as you always work under the assumption that you will from time to time be sent false data from cheaters you can use remote events and still have a secure game. The problems arise when you just make a remote event directly do something without any verification.

Server script:

local Players = game:GetService('Players')
local b = game:GetService('BadgeService')

local sentences = {
	"Badger Badger Mushroom",
	"Ahah! You'll be here forever.",
	"No badge here.",
	"Unlucky, no badge for you.",
	"Badge For You",
	"How long have you been here?",
	"This is what true pain is."
}

Players.PlayerAdded:Connect(function(Player)
    local PlayerGui = Player:WaitForChild('PlayerGui')
    local Button = PlayerGui.ScreenGui.Button -- Change ScreenGui.Button to all the UI info.
    local TextBox = Button.Parent.TextBox -- Again, change all that to the correct UI info.

    Button.MouseButton1Down:Connect(function()
        local text = sentences[math.random(1, #sentences)]
        TextBox.Text = text

        if text == "Badge For You" then
            b:AwardBadge(Player.UserId, 2125641650)
        end
    end)
end)

That should work, but it’s untested! :smile:

1 Like

It didn’t work, but I think I did something wrong! Let me keep trying and I’ll tell you if it works or not.

Firstly, you should create a remote event with name “AwardBadge” in the ReplicatedStorage.

LocalScript:

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

local GuiButton = script.Parent 

local sentences = {
	"Badger Badger Mushroom",
	"Ahah! You'll be here forever.",
	"No badge here.",
	"Unlucky, no badge for you.",
	"Badge For You",
	"How long have you been here?",
	"This is what true pain is.",
}

GuiButton.MouseButton1Click:Connect(function()
   local sentence = sentences[math.random(#sentences)]
   GuiButton.Text = sentence

   if sentence == "Badge For You" then
	   AwardBadge:FireServer()
   end	
end)

Script:

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

local AwardBadge = ReplicatedStorage.AwardBadge
local BadgeId = 2125641650

Awardbadge.OnServerEvent:Connect(function(player)
   local success, badgeInfo = pcall(function()
	  return BadgeService:GetBadgeInfoAsync(BadgeId)
   end)

   if success and badgeInfo.IsEnabled then
      local success, result = pcall(function()
	     return BadgeService:AwardBadge(player.UserId, BadgeId)
	  end)
   end
end)

With pcall you can handle errors like

if not success then
   --Handle error
end

But thats not necessary

2 Likes

It worked! Thanks so much for helping.

1 Like

This is how it should be done, but I would recommend picking the string on the server. The reason is so that cheaters with tools can’t give themselves an award by writing their own script to send the correct string. In terms of security issues this is really a minor thing since an award isn’t really all that game breaking to be falsely awarded, but it’s best to get in the habit of making all important decisions on the server so you don’t accidentally create this type of issue with more important things.

In this case the remote event should let the server know the button was pressed. The server should pick the random string and check if it awards a badge and send that string back through the remote event so that it can display it. That way even if someone writes their own code to simply fire the remote event they won’t get the badge. (Though unless you add a cooldown or something they could still spam it to get the award quickly).

It’s best to always write your code under the assumption that people are writing their own local scripts.

1 Like

You’re right. I didn’t think about security.

1 Like