So I want to make a text box, that if they get the right number, it will do something, but it doesnt work
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!
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 :
Convert to local Script
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
ScreenGui.SurfaceGui.TextButton.MouseButton1Click;Connect(function()
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.
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.
-- 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)
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()
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.