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

I want to make a part that when it is clicked, a random sentence appears and if you get a certain sentence (here it is “Badge For You”), it award a badge.

The SurfaceGui with a child called “GuiButton” which is just a TextButton, doesn’t change and it also doesn’t reward the badge. I’m not good at scripting so please help… script is below.

I already tried to look on YouTube and DevForum but I could only find random sentence generators and none that told me how to award badges.

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()
	print(sentences[math.random(#sentences)])

		if GuiButton.Text == "Badge For You" then
		
		local id = 2125641650
			print("Awarding BadgeID: " .. id .. " to UserID: " .. game.Players.LocalPlayer.userId)
			local b = game:GetService("BadgeService")
			b:AwardBadge(game.Players.LocalPlayer.userId, id)
		end	
	end)
2 Likes

Where are you updating gui button text? You are printing a random one, but since you aren’t giving GuiButton.Text the value it never technically changed unless you do that elsewhere. So the if statement will always fail with the given code.

Instead of printing it set GuiButton.Text to the value and it should work.

In other words change this line

print(sentences[math.random(#sentences)])

To this

GuiButton.Text = sentences[math.random(#sentences)]
1 Like

your problem, your only giving math.random one argument. try saying:

sentences[math.random(0, #sentences)].

The math.random function finds a random number between two numbers, the first and second number. if you give it one argument, it won’t know where to look!

1 Like

“ When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]” math

One argument will work, it will just assume the first argument is 1 and pick a number between 1 and m.

I changed them both, I have this

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()
	GuiButton.Text = sentences[math.random(7, #sentences)] -- Don't know if I was supposed to change it to 7 lol

		if GuiButton.Text == "Badge For You" then
		
		local id = 2125641650
			print("Awarding BadgeID: " .. id .. " to UserID: " .. game.Players.LocalPlayer.userId)
			local b = game:GetService("BadgeService")
			b:AwardBadge(game.Players.LocalPlayer.userId, id)
		end	
	end)

but it still doesn’t work when I click it.

Yes, but I would say put in the first argument (just in-case) :slight_smile:

You would need to set the first number in random as 1. Or leave out that number as your original code would assume that anyways. Basically put in the code exactly as I had it because that would work. It’s shorthand for the code Denver recommended.

It now works with showing the sentences, but when the sentence “Badge For You” it shows ups, it doesn’t award the badge.

Thanks, changed it to that too

try setting it to 0. that may help.

I don’t usually award badges, but I’m rather certain you can’t do so in a local script. You’ll have to use remote events on the server. But you would also want to move the random string there as well so they can’t make it up.

Lua lists start at 1. 0 would cause an out of bounds error.

View this article.

math.random(a, b)

returns a value between a and b-1.

local GuiButton = script.Parent 
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."
}

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

		if text == "Badge For You" then
		    local id = 2125641650
			    print("Awarding BadgeID: " .. id .. " to UserID: " .. game.Players.LocalPlayer.userId)
			    local b = game:GetService("BadgeService")
			b:AwardBadge(game.Players.LocalPlayer.userId, id)
		end	
	end)
1 Like

This is most likely the reason why. Roblox would be a very insecure site if you could award badges on the client. Exploiters and hacked clients could easily hack your game.

Do everything securely on the server.

I know how math.random works. It is inclusive which means if you say math.random(0,#list) it could return 0.

If you try list[0] it would error since the first index is 1. So you need to start with 1.

Other languages start at 0, but lua is weird.

Yes, lua is weird, but when using math.random() lua will NEVER return the first argument.

It only returns a number BETWEEN the first argument, and on the second. so if you said:

print(math.random(0, 1))

It would always return 1.

Creating a RemoteEvent is even more dangerous. The hacker could connect the event to a script of their own, and harm the game more. Awarding a badge on the client is the least of our worries, who cares if someone hacks a badge? It’s not worth monetary value.

All that aside, that was a problem on their end. It does need to be called on a server-script, so there will need to be some way of getting it to that.

What I mean is that it doesn’t matter WHAT you’re trying to do on the client, make sure that it’s inaccessible from any exploiters.

You don’t want your game to get hacked in the first place. Do you?