Scripting help!

Hey! As people know Im not so bright with scripting. Anyone have a working GUI for Twitter codes? I want to award someone a tool when they redeem a code. Ty :^] ~Maseybb

4 Likes

Hi there, I don’t have one on hand at the moment but I can explain how you can go about doing it.

Start by having a TextBox element. Next, make a LocalScript inside of the TextBox and insert the following code, this specific example will rely on FocusLost.

local Core = {
-- variables
 TextBox = script.Parent,
-- methods
CheckCode = function(self,EnteredCode)
 if EnteredCode == "Desired Code" then
  local Tool = --[[Location Of Tool]]:Clone()
  Tool.Parent = game.Players.LocalPlayer.Backpack
 end
end
};

Core.TextBox.FocusLost:Connect(function()
 Core:CheckCode(Core.TextBox.Text)
end

Wrote this pretty quick so there might be a few kinks but this is one way of going about making one.

3 Likes

Wow that’s a weird way of scripting lol
Remember that he is a beginner and there is really no reason to make it more complex with tables and methods :wink:

Further simplified version is:

local TextBox = script.Parent -- saves the text box that holds the script

TextBox.FocusLost:Connect(function() -- when the player finishes typing
    local Text = TextBox.Text -- save the text to be used multiple times later
    if Text == "Code" then -- check to see if it is equal to the code
        -- do something
    elseif Text == "OtherCode" then
        -- do something else
    end
end)
3 Likes

Yeah mostly out of habit lol, use Emerald’s version.

2 Likes

Thank you both :^]

You should really check for the enterpressed parameter in focuslost, otherwise a user might accidently lose focus and write in the wrong code. Perhaps edit the post to better explain.

1 Like

That won’t change anything, though. The code isn’t destructive, so accidentally un-focusing it won’t cause any issues.

2 Likes

It’s still a better practise. If you have cusom logic for getting the code wrong this will effect it. This also saves extra code being run when it isn’t needed. I know these things are small but I always like to optimize my code.

2 Likes

Yeah but this is the case of a beginner where it’s best to keep it simple.

Too much information is just hard imo, especially when it is all being taught in one piece of potentially daunting and already-confusing code

Idk whatever :wink:

2 Likes