I don't understand why this script isn't working

I was trying to make it so that text shows up when a player types in a wrong code but it does not seem to work, the script is a local script but whenever i try to make appear when a wrong code is typed in, nothing happens. The print statment shows up but not the function. Could someone please help me?

local RS = game:GetService("ReplicatedStorage")
local Codes = {"Cash", "Money", "Code"}
local Event = RS.CodeEvent
local UI = game.StarterGui.CodeGUI.Frame:WaitForChild("RedeemedText2")

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text == Codes[1] then
		print("Event Fired 1")
		Event:FireServer(1000, Codes[1])
	else
		if script.Parent.TextBox.Text == Codes[2] then
			print("Event Fired 2")
			Event:FireServer(2500, Codes[2])
		else
			if script.Parent.TextBox.Text == Codes[3] then
				print("Event Fired 3")
				Event:FireServer(5000, Codes[3])
			end
		end
	end
end)

local function ShowGUI()
	UI.Visible = true
	wait(4)
	UI.Visible = false
end

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text ~= Codes then
		print("No Code!")
		ShowGUI()
	end
end)

You created two functions for when the player clicks on the button which is not needed.
I’m no expert but try this out

local RS = game:GetService("ReplicatedStorage")
local Codes = {"Cash", "Money", "Code"}
local Event = RS.CodeEvent
local UI = game.StarterGui.CodeGUI.Frame:WaitForChild("RedeemedText2")

local function ShowGUI()
	UI.Visible = true
	wait(4)
	UI.Visible = false
end

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text == Codes[1] then
		print("Event Fired 1")
		Event:FireServer(1000, Codes[1])
	        elseif script.Parent.TextBox.Text == Codes[2] then
			print("Event Fired 2")
			Event:FireServer(2500, Codes[2])
		elseif script.Parent.TextBox.Text == Codes[3] then
				print("Event Fired 3")
				Event:FireServer(5000, Codes[3])
                else 
                     print("No Code!")
                     ShowGUI()
			end
		end
	end
end)


1 Like

A classic case of the StarterGui issue, oof

Whenever you define the StarterGui, that’s only replicated to those that first join the game (Think of it as like the Server-Side version of the PlayerGui)

What you want instead, is the PlayerGui which is only visible to that individual LocalPlayer instead

There are a couple of ways you can define the PlayerGui, either by doing script.Parent & referencing it that way, or LocalPlayer:WaitForChild("PlayerGui")

local RS = game:GetService("ReplicatedStorage")
local Codes = {"Cash", "Money", "Code"}
local Event = RS.CodeEvent
local Plr = game.Players.LocalPlayer
local PlrGui = Plr:WaitForChild("PlayerGui")
local CodeGui = PlrGui:WaitForChild("CodeGui")
local UI = CodeGUI:WaitForChild("Frame"):WaitForChild("RedeemedText2")

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text == Codes[1] then
		print("Event Fired 1")
		Event:FireServer(1000, Codes[1])
	elseif script.Parent.TextBox.Text == Codes[2] then
		print("Event Fired 2")
		Event:FireServer(2500, Codes[2])
	elseif script.Parent.TextBox.Text == Codes[3] then
		print("Event Fired 3")
		Event:FireServer(5000, Codes[3])
	end
end)

local function ShowGUI()
	UI.Visible = true
	wait(4)
	UI.Visible = false
end

script.Parent.EnterButton.MouseButton1Click:Connect(function()
	if script.Parent.TextBox.Text ~= Codes then
		print("No Code!")
		ShowGUI()
	end
end)
1 Like

Yeah I have tried that but It does not seem to work. It does not give me any error, it just doesn’t show up.

Can you check your Output for anything else that might’ve printed back in your function?

Okay so it does work now but the text shows every time, even if you type in the correct code.

local UI = game.StarterGui.CodeGUI.Frame:WaitForChild("RedeemedText2")

Change this to:

local UI = game.Players.LocalPlayer.PlayerGui.CodeGUI.Frame:WaitForChild("RedeemedText2")

1 Like

Everything works fine now just had to remove the mousebutton1click and place code inside that inside the first mouse button 1 click function.