FocusLost() not working

I am using the FocusLost() function for a textbox, but theres a problem when firing a remoteevent, it simply does not fire. I checked on the server if the remote fires and prints something, but it doesnt.
local script:

local RemoteFolder = game.ReplicatedStorage:WaitForChild("ComputerEvents")

script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		if script.Parent.Text == "Island_Tower_Activate" then
			game.ReplicatedStorage.ComputerEvents.UserActivateTower:FireServer()
		end
	end
end)

script:

game.ReplicatedStorage.ComputerEvents.UserActivateTower.OnServerEvent:Connect(function()
	workspace.Tower2.UserActivated.Value = true
	print("taken")
end)

Any help?

I don’t think you can use Focus Lost on text Label ,It’s only for Textbox I think

My bad, i mispelled textlabel with textbox

1 Like

Are you trying to check if the player pressed enter?

If so you need to use UserInputService and check the textbox text

1 Like

I do not think itst hat problem, im using that code from roblox developer hub, so yeah

Add print statements in the event to see if they get through, before the enterpressed statement, after the enter pressed statement, and after the text checking

1 Like

Try removing the enter pressed check…

So looks like the code fires, but somewhere in the server script is the problem, because both of the prints are in the output.
server script:

game.ReplicatedStorage.ComputerEvents.UserActivateTower.OnServerEvent:Connect(function()
	workspace.Tower2.UserActivated.Value = true
	print("taken")
end)

Both? I mentioned 3 prints to put, before the enterpressed statement, after the enterpressed statement, and after the statement when it checks the text. Are you sure you wrote Island_Tower_Activate correctly when testing?

Replace ‘workspace’ by

game:GetService("Workspace")

then try running your script again.

Yes yes, i am very sure of that. the last print i put was right under the FireEvent() function, so not the local script is the problem

edit: i also checked what the server should do when it receives the event in the command bar, on run mode. the function works. so there may be a problem in getting the event…??

That thing you mentioned and workspace are the same thing though, that change will do nothing

@octav20071 Do you have any code above the OnServerEvent that could be causing an issue?

I do have, but it does not seem important. full code:

local value = workspace.Tower2.Activated
local value2 = workspace.Tower2.UserActivated
local interface = workspace.Interface.Interface
local randomval = math.random(60,240)

while true do
	print(randomval)
	wait(randomval)
	if value.Value == true and value2.Value == true then
		interface.ClickDetector.MaxActivationDistance = 32
		value.Value = true
	else
		print("tower is deactivated")
	end
	wait(240)
	if value.Value == false and value2.Value == false then
		print("good job deactivating the tower")
	else
		interface.ClickDetector.MaxActivationDistance = 0
		value.Value = false
		print("xana won")	
	end
end

game.ReplicatedStorage.ComputerEvents.UserActivateTower.OnServerEvent:Connect(function()
	game:GetService("Workspace").Tower2.UserActivated.Value = true
	print("taken")
end)

You have the code under an infinite loop, that’s why it doesn’t work, put

game.ReplicatedStorage.ComputerEvents.UserActivateTower.OnServerEvent:Connect(function()
	game:GetService("Workspace").Tower2.UserActivated.Value = true
	print("taken")
end)

above the loop

1 Like

Thanks! I had no idea that this little thing can affect the whole code!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post! And yea, if you have code under an infinite loop that doesn’t break, it wont work because the script is stuck in the while loop, you either need to coroutine the loop or just put it as the last thing

1 Like

Use coroutines in the future if you need to use a while loop in your code or you’re going to have a really bad time.

https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

1 Like