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

the elseif is not working anyone notice anything wrong?

local player = game.Players.LocalPlayer
local RE = game.ReplicatedStorage.EnterCode
local codes = player:WaitForChild("Codes")

--Optional if you want your code to have an expiration
local date = os.date("*t", os.time())

--Codes
local codelist = {
    code1 = "UPDATE3", -- Always put a comma every timne you're going to add another code
    code2 = "FIREY"
}

script.Parent.MouseButton1Click:Connect(function()

    if script.Parent.Parent.CodeHandler.Text == codelist.code1 then 
            if not codes:FindFirstChild("Code1") then -- This one here
                RE:FireServer(500, "Code1") -- The string must be the same as in the top. For now I can reward the player, cash. Until I can learn how to save tools.
                script.Parent.Text = "Code redeemed successfully!"
                script.Parent.Parent.CodeHandler.Text = ""
                wait(2)
                script.Parent.Text = "Redeem Code"
            else
                script.Parent.Text = "Code already redeemed!"
                wait(2)
                script.Parent.Text = "Redeem Code"
            end
        else
            script.Parent.Text = "Code invalid!"
            script.Parent.Parent.CodeHandler.Text = ""
            wait(2)
            script.Parent.Text = "Redeem Code"
        end
        
        
        
         elseif script.Parent.Parent.CodeHandler.Text == codelist.code2 then -- Your second code. So if our textbox matches the code in our dictionary...
            if not codes:FindFirstChild("Code2") then -- This one here
                RE:FireServer(150, "Code2") -- The string must be the same as in the top. For now I can reward the player, cash. Until I can learn how to save tools.
                script.Parent.Text = "Code redeemed successfully!"
                script.Parent.Parent.CodeHandler.Text = ""
                wait(2)
                script.Parent.Text = "Redeem Code"
            else
                script.Parent.Text = "Code already redeemed!"
                wait(2)
                script.Parent.Text = "Redeem Code"
            end
        else
            script.Parent.Text = "Code invalid!"
            script.Parent.Parent.CodeHandler.Text = ""
            wait(2)
            script.Parent.Text = "Redeem Code"
        end
end)

3 comments

0

On waht line?

0

on Line 37 it says expected end to close line 14 but when i tried putting a end it doesnt work

0

elseif's always go before else's, the hierarchy goes if ... then ... -> elseif ... then ... -> else ...-> end . You can put as many elseif's as you want before the else. You don't need an else to end the elseif's.

1 answer

The last else block, including its end, was copied to line 28 without an if to go with it. It being there doesn't make much sense being there (was it put there by accident?). Removing lines 28-33 should fix it.

With that out of the way... there's also 2 things that wouldn't cause it not to work, but could become bigger problems later.

Security problems

You're having the checking done on the client, and firing a remote with the prize to give: A. An exploiters could fire the remote on their own, bypassing the checks (and maybe with huge amounts of cash too!) B. An exploiter who's able to read the contents of codelist could leak every code in the game

To fix this, a remote function for dealing with a code could be made and the checks moved to a server-side script. The on-clicked function could then just invoke the RF with the text, and do something based on the response it gets.

Repetitiveness

The if and elseif paths of the if are almost the same except for the code name and the money rewarded. While not that bad now, things would be very repetitive if you added more codes. Too much repetition can make mistakes harder to find (here's a relevant animation).

One way of dealing with this would be to have table accessed by code text that gives info about the code (or nil if the text was not a valid code). This would also make it very easy to add or remove codes - you would just have to edit the table.

local codes = {
	UPDATE3 = {
		id = "Code1";
		cash = 100;
	}; 
	
	FIREY = {
		id = "Code2";
		cash = 100;
	};
}

Dealing with the issues

With the above two issues thought about, one way the system on the server's side could be done would be:

function codeRedeemingRemote.OnServerInvoke(player, codetext)
	local codeInfo = codes[codetext] -- getting the info it it's valid, or nil if it's not

	-- `if codeinfo == nil then` would also work
	if not codeInfo then
		return "Code invalid!" -- exiting early on failure, saying why
	end
	
	if redeemedCodes:FindFirstChild(codeinfo.id) then
		return "Code already redeemed"
	end
	
	-- give the player `codeInfo.cash` worth of cash here
	-- add a tag to redeemedCodes based on `codeInfo.id` too
	
	return "Code redeemed successfully!"
end)

On the client side you could tell the player to wait for it to redeem, do codeRedeemingRemote:InvokeServer(codeHandler.Text) and deal with the result. (Instead of strings here you could also do enum-likes for the same reasons Roblox switched to enums, but this reply is a bit long already.)

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