Help: The code lines doesn't work when pressing E

Hello, I am making a hiding spot exiting function using the user input service, But I encountered a problem with the code, it doesn’t work and the output doesn’t say anything.

== LocalScript ==

local UserInputService = game:GetService("UserInputService")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if UserInputService.KeyboardEnabled then
			UserInputService.InputBegan:Connect(function(input)
				if input.KeyCode == Enum.KeyCode.E then
					game.ReplicatedStorage.GoOutLocker:FireServer()
					print("E")
				end
			end)
		end

		if UserInputService.TouchEnabled then
			local UnhideButton = game.Players.LocalPlayer.PlayerGui:WaitForChild("Screen").Mobile
			UnhideButton.Visible = true
			UnhideButton.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.GoOutLocker:FireServer()
			end)
		end
	end)
end)

Can anyone fix this problem, I would appreciate it.

Greetings!
I`ve noticed some issues with your code, so I edited it to solve them.
Here it is

-- Services
local UserInputService = game:GetService("UserInputService")

-- UIs
local UnhideButton = game.Players.LocalPlayer.PlayerGui:WaitForChild("Screen"):WaitForChild("Mobile")

-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()


UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("Pressed \'E\'!")
		game.ReplicatedStorage.GoOutLocker:FireServer()
		
	end
end)

if UserInputService.TouchEnabled then
	UnhideButton.Visible = true
	UnhideButton.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.GoOutLocker:FireServer()
		
	end)
end

Make sure that this is running on a local script, and put it on StarterPlayer > StarterCharacterScripts or StarterPlayerScripts to take effect and run.

local GoOutLockerEvent = game.ReplicatedStorage.GoOutLocker

GoOutLockerEvent.OnServerEvent:Connect(function(Player)
	print("Signal!")
	
end)

Make sure this one is on a Server-side script, ServerScriptService would be a great place.

If you want to discuss why your code didn’t run, I am happy to explain it for you.
Have a nice day :blue_heart:

1 Like

Explaining the issue, why the code didn’t work?

Aha, I am glad you asked.
You did yield the code to run once an event is triggered.
That event was a player being added,

image

The script runs after the player is already added, and the event is already triggered before the script runs.
So the script just misses the event and waits for another player to enter and then waits for his character to load.
But since you are doing that in a place testing only with 1 player entering (I guess), no second player is entering after you. so it will just yield there.

I hope you find this helpful, I tried to simplify it as much as I can :sweat_smile:

1 Like

Oh ok, and thanks for helping! :heart:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.