TextBox Problem

So I want to make a text box, that if they get the right number, it will do something, but it doesnt work :confused:
This is the code:

local TextBoxText = game.StarterGui.ScreenGui.SurfaceGui.TextBox.Text

local function CheckCode()
	if TextBoxText == 123456 then
		game.Workspace.Barrier:Destroy()
	end
end
game.StarterGui.ScreenGui.SurfaceGui.TextButton.MouseButton1Click:Connect(function()
	print(TextBoxText)
	CheckCode()
end)

This is a script and not a local script, cuz I want to make the barrier disappear for everyone
Thanks! Hope someone can solve this!

It’s not possible to access GUI related stuff using a normal script. You should change it to local.

you can make the local script check if the button pressed and then send a Signal to the server to do whatever you want

3 Likes

There is many problems with this code, Firstly StarterGuiService is replicated to clients as PlayerGui inside the player instance in Player Service, Secondly detecting user input from Server sided scripts isn’t possible. Make these changes :

  1. Convert to local Script

local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
ScreenGui.SurfaceGui.TextButton.MouseButton1Click;Connect(function()
2 Likes

I thought it was possible? I might’ve been wrong.

The issue I see is this:

You are attaching the event to the StarterGui, not the cloned GUI within each player.

But as @black1shadow1048 stated, you should definitely do this in a local script. Utilize remote-events as well.

2 Likes

Oh ok, then I do this thing?

print(TextBoxText)
CheckCode()

Can it still be played server side even tho its a local script?

Ah okay, then how to make a signal to the server?

I already linked an article in the roblox wiki which will help you understand remote events better

1 Like

If you want the barrier to dissapear for everyone then you will need to utilize RemoteEvents :FireServer(), You will also need to verify this is a valid player that can delete barriers and then on the server side you :Destroy() the barrier.

2 Likes

OH I should use FireServer, right?

Yea, thanks for that information

If you just destroy the barrier when the remote event is fired, someone would probably be able to fire the remote event using exploits and destroy the barrier without knowing the correct passcode, you should check if the code is correct from the server instead of just the client.

1 Like

local script:

-- You should put the script inside the textbutton
local Button = script.Parent
-- Put the textbox inside the textbutton as well
local TextBox = Button.TextBox
-- You need to put a RemoteEvent inside the replicated storage. otherwise it won't work
local RemoteEvent = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")

Button.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer(TextBox.Text)
end)

Server script:

local RemoteEvent = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(player,Text )
	if Text == "12345" then
		--Put your code here
	end
end)

hope this helped

3 Likes

It should be like this right if im not dumb
The script in ServerScriptService:

local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("CheckCode")
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
local TextBoxText = ScreenGui.SurfaceGui.TextBox.Text

local function CheckCode()
	if TextBoxText == 123456 then
		Player.Workspace.Barrier:Destroy()
	end
end
ScreenGui.SurfaceGui.TextButton.MouseButton1Click:Connect(function()
	RemoteEvent.OnServerEvent:Connect(CheckCode)
end)

The script in StarterCharacterScripts:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("CheckCode")


RemoteEvent:FireServer()

What? I already stated this to the OP

2 Likes

Oh ok, let me try this real quick

Oh sorry, I didn’t know what you meant by this is a valid player that can delete barriers. I just wanted to make sure OP knew that his game could be exploited if the passcode was stored and checked on the client.

BROOOO, this works perfectly, thank you so much!
Thank you to all other person that help me!
@black1shadow1048
@Velliaan
@ifkpop
@oopacity
Thank you so much!

2 Likes