New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Problem when using a Global variable?

Asked by 2 years ago
Edited 2 years ago

Hello, so I have a script in the workspace that contains this:

_G.codes = {
"TEST1",
"TEST2",
"TEST3"
}

And a LocalScript in the StarterGui that contains this:


local target = script.Parent.code function suc() script.Parent:TweenPosition( UDim2.new(0, -450,0.098, 0), "Out", "Elastic", 2.1, true, function() script.Parent.Visible = false end) target.Text = "<enter code here>" end function unsuc() target.Text = "Code Invalid!" wait(3) script.Parent:TweenPosition( UDim2.new(0, -450,0.098, 0), "Out", "Elastic", 2.1, true, function() script.Parent.Visible = false end) target.Text = "<enter code here>" end script.Parent.submit.MouseButton1Down:Connect(function() while _G.codes == nil do wait() end --CODE TWO (Works) if target.Text == _G.codes[2] then target.Text = "Code Redeemed!" print("Test") local p = game.Players.LocalPlayer local b = game:GetService("BadgeService") b:AwardBadge(p.userId, 918606619) wait(3) suc() else unsuc() end --CODE ONE (Doesn't Work) if target.Text == _G.codes[1] then target.Text = "Code Redeemed!" local player = game.Players.LocalPlayer player.leaderstats.XP.Value = player.leaderstats.XP.Value + 100 wait(3) suc() else unsuc() end --CODE THREE (Doesn't Work) if target.Text == _G.codes[4] then target.Text = "Code Redeemed!" print("test") wait(3) suc() else unsuc() end end)

And for some reason when I enter in the correct code and hit submit, it returns the unsuc()function. Also, Code 2 works fine and the others dont. Help please!

1 answer

Log in to vote
2
Answered by 2 years ago

I don't see a use of global variables in this. You can have a module script that holds the codes using an array, and use a global script to require the module.

Inside the localscript, make a function that gets called when submit is clicked. Then invoke a remotefunction giving the input from the textbox as an argument.

Inside the global script, have a function that handles the invoke and make it call a function inside the module script to get the codes, if the input is a valid code (Check via for loop) then return true.

Lets say the remotefunction is located inside ReplicatedStorage

Since examples are worth a thousand words, here you go

Global Script

local module = require(script.Module)

game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(input)
return module:verifyCode(input)
end

Module Script Inside global script

local module = {}

local codes = {
"Test", "Test1", "Test2"
}

function module.verifyCode(input)
for _,v in pairs(codes) do
    if v == input then
        return true
    end 
end
-- If all fails
return false
end

return module

Local Script

script.Parent.submit.MouseButton1Click:connect(function()
if game.ReplicatedStorage.RemoteFunction:InvokeServer(script.Parent.code.Text) == true then
    -- CODE
end
end
Ad

Answer this question