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)
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.
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!
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)
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.
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.
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)
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.
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.