Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Another problem with my code gui? [Unanswered]

I decided to add another code to my gui, the first code (Points) works great. The second, not so much. I put in "MorePoints", and it does nothing at all. It just says "MorePoints". Any ideas as to why this is happening? I'm also getting no errors whatsoever.

local box = script.Parent.Parent.CodeBox
local plr = script.Parent.Parent.Parent.Parent
local redeemed = false

box.FocusLost:connect(function()
	if box.Text == "Points" or "MorePoints" and redeemed == false then
		if box.Text == "Points" then
			redeemed = true
	    	box.Text = "Code Accepted!"
			plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 50
	    	wait(1)
	    	box.Text = "Redeem Code"
		elseif box.Text == "MorePoints" then
			redeemed = true
	    	box.Text = "Code Accepted!"
			plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 100
	    	wait(1)
	    	box.Text = "Redeem Code"
		elseif box.Text == "Points" or "MorePoints" and redeemed == true then
			box.Text = "Already Used!"
			wait(1)
			box.Text = "Redeem Code"
		else
	    	box.Text = "Code Declined!"
	    	wait(1) 
	    	box.Text = "Redeem Code"
		end
	end
end)

2 answers

✓ Accepted answer

marcoantoniosantos3 fixed up your 'if' statements correctly. If the first thing you type is "MorePoints", does it work?

I expect that what's happening is that you're expecting to be able to type in "Points" and then "MorePoints" after that, but the script is rejecting that because "redeemed" is already "true".

The solution is to have another variable, like "morePointsRedeemed". If you plan on having more than two of these lines, you should switch to lists and dictionaries:

validCodes = {"Points", "MorePoints"} --expand as desired
redeemed = {} --Dictionary; key is code, value is true for redeemed or nil for not redeemed
redeemFunction = {
	function() plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 50 end, --Points
	function() plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 100 end --MorePoints
} --expand as desired to mirror the "validCodes" list (so that the first function redeems the first code in the list, etc.)


local box = script.Parent.Parent.CodeBox
local plr = script.Parent.Parent.Parent.Parent

box.FocusLost:connect(function()
	local validCode = false
	local functionToUse = nil --the function to call to redeem the code
	for i = 1, #validCodes do
		if box.Text == validCodes[i] then
			validCode = true
			functionToUse = redeemFunction[i]
			break
		end
	end
	if validCode then
		if not redeemed[box.Text] then
			redeemed[box.Text] = true
			box.Text = "Code Accepted!"
			functionToUse()
			wait(1)
			box.Text = "Redeem Code"
		else
			box.Text = "Already Used!"
			wait(1)
			box.Text = "Redeem Code"
		end
	else
		box.Text = "Code Declined!"
		wait(1) 
		box.Text = "Redeem Code"
	end
end)

2 comments

0

Players.Player.PlayerGui.RedeemCode.CodeBox.CodeChecker:4: attempt to index global 'plr' (a nil value)

0

Nmv, thanks allot sir :D

Not sure about this fix but:

local box = script.Parent.Parent.CodeBox
local plr = script.Parent.Parent.Parent.Parent
local redeemed = false

box.FocusLost:connect(function()
    if (box.Text == "Points" or box.Text == "MorePoints") and redeemed == false then
        if box.Text == "Points" then
            redeemed = true
            box.Text = "Code Accepted!"
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 50
            wait(1)
            box.Text = "Redeem Code"
        elseif box.Text == "MorePoints" then
            redeemed = true
            box.Text = "Code Accepted!"
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 100
            wait(1)
            box.Text = "Redeem Code"
        elseif (box.Text == "Points" or box.Text == "MorePoints") and redeemed == true then
            box.Text = "Already Used!"
            wait(1)
            box.Text = "Redeem Code"
        else
            box.Text = "Code Declined!"
            wait(1) 
            box.Text = "Redeem Code"
        end
    end
end)

1 comment

0

Nupe.

This discussion is preserved as it was. New answers, comments, and votes are closed.