Looking to Create a Function For a Built-In Nohboard

it still doesn’t work anyways, I used a print command to see if the function was running and it didn’t event print :sob:

Te lo explicaré en español porque ya me molesta mucho traducir el mensaje, busca el traductor y ya está si quieres aprender, imagina lo siguiente te llega un avión de papel a la vez y ese avión de papel solo puede tener como mensaje una tecla, al llegar ese avión de papel esa tecla puede ser cualquiera en este caso supongamos que la tecla a entonces al ser la tecla a y es un solo avión de papel pues va a detectar que es a sería tonto verificar si la tecla a es igual a la tecla e así que no es necesario verificar las otras condiciones si solo se cumple una. Si por alguna razón te tiran dos aviones de papel con diferentes teclas a la vez pues ten en cuenta que el sistema de roblox se va a encargar de que solo te llegue una a la vez pues así funciona el sistema de eventos.

Show You code please
Please
Please

Hmm that’s interesting, but only sending one key input at a time is a bit concerning to me because it could happen fairly often where someone presses two keys at the same time. I’ll still keep it this way though and see how it workedi n the end

This is all of the code that I have right now:

local starterGui = game:GetService("StarterGui")
local userInput = game:GetService("UserInputService")
local nohW = starterGui.NohboardW
local nohA = starterGui.NohboardA
local nohS = starterGui.NohboardS
local nohD = starterGui.NohboardD
local nohSpace = starterGui.NohboardSpace

nohW.Enabled = false
nohA.Enabled = false
nohS.Enabled = false
nohD.Enabled = false
nohSpace.Enabled = false

function checkKeys()
	print("test")
	userInput.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.W then
			nohW.Enabled = true
		else if input.KeyCode == Enum.KeyCode.A then
			nohA.Enabled = true
			else if input.KeyCode == Enum.KeyCode.S then
					nohS.Enabled = true
				else if input.KeyCode == Enum.KeyCode.D then
						nohD.Enabled = true
					else if input.KeyCode == Enum.KeyCode.Space then
							nohSpace.Enabled = true
						end
					end
				end
			end
		end
	end)
	userInput.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.W then
			nohW.Enabled = false
		else if input.KeyCode == Enum.KeyCode.A then
			nohA.Enabled = false
			else if input.KeyCode == Enum.KeyCode.S then
					nohS.Enabled = false
				else if input.KeyCode == Enum.KeyCode.D then
						nohD.Enabled = false
					else if input.KeyCode == Enum.KeyCode.Space then
							nohSpace.Enabled = false
						end
					end
				end
			end
		end
	end)
end

checkKeys()

You can neaten this up with a simple table and using the KeyCode as the key to the table and the Gui as the value.

local starterGui = game:GetService("StarterGui")
local userInput = game:GetService("UserInputService")

local keycodeToGui = {
	[Enum.KeyCode.W] = starterGui.NohboardW,
	[Enum.KeyCode.A] = starterGui.NohboardA,
	[Enum.KeyCode.S] = starterGui.NohboardS,
	[Enum.KeyCode.D] = starterGui.NohboardD,
	[Enum.KeyCode.Space] = starterGui.NohboardSpace,
}

-- we can loop through a table and set all of them to false
-- save the copy and paste
for _, ui in keycodeToGui do
	ui.Enabled = false 
end

function checkKeys()
	print("test")
	userInput.InputBegan:Connect(function(input)
		if keycodeToGui[input.KeyCode] then -- check if the key exists
			keycodeToGui[input.KeyCode].Enabled = true -- set the respective gui to be enabled
		end
	end)
	userInput.InputEnded:Connect(function(input)
		if keycodeToGui[input.KeyCode] then
			keycodeToGui[input.KeyCode].Enabled = false
		end
	end)
end

checkKeys()
1 Like

Uy bro ajajjajaajaj, is easy, first, i no speak English, antes que nada debes hacer esto.

If input.KeyCode == a then
–code
Elseif input.KeyCode == e then

—code

end

La solución de @kingerman88 es correcta y buena

@ThatSinisterGuy si necesitas que te explique el código me dices, pero no te lo diré en english

Wow yeah this looks so much neater, but for some reason it still doesn’t work and I’m not too sure why. Is it fine that its a LocalScript in StarterPlayerScripts?

image
No matter what I do the GUI stays on and won’t budge no matter what I do.

Also a bit of info, the “test” doesn’t even print so it most likely has something to do with the function.

nevermind it does lol im stupid

Use local function( limite de caracteres)

like this?

local checkKeys = function()
	userInput.InputBegan:Connect(function(input)
		if keycodeToGui[input.KeyCode] then -- check if the key exists
			keycodeToGui[input.KeyCode].Enabled = true -- set the respective gui to be enabled
		end
	end)
	userInput.InputEnded:Connect(function(input)
		if keycodeToGui[input.KeyCode] then
			keycodeToGui[input.KeyCode].Enabled = false
		end
	end)
end

checkKeys()

Ah, the reason lies in the fact that you are changing the StarterGui’s version of the UI. StarterGui holds UIs that will be cloned into player.PlayerGui, but otherwise serves no functional purpose. Think of the UI stored in there as templates.

It’s a pretty easy fix, just swap out starterGui with player.PlayerGui

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local userInput = game:GetService("UserInputService")

local keycodeToGui = {
	[Enum.KeyCode.W] = playerGui:WaitForChild("NohboardW"),
	[Enum.KeyCode.A] = playerGui:WaitForChild("NohboardA"),
	[Enum.KeyCode.S] = playerGui:WaitForChild("NohboardS"),
	[Enum.KeyCode.D] = playerGui:WaitForChild("NohboardD"),
	[Enum.KeyCode.Space] = playerGui:WaitForChild("NohboardSpace"),
}

-- we can loop through a table and set all of them to false
-- save the copy and paste
for _, ui in keycodeToGui do
	ui.Enabled = false 
end

function checkKeys()
	print("test")
	userInput.InputBegan:Connect(function(input)
		if keycodeToGui[input.KeyCode] then -- check if the key exists
			keycodeToGui[input.KeyCode].Enabled = true -- set the respective gui to be enabled
		end
	end)
	userInput.InputEnded:Connect(function(input)
		if keycodeToGui[input.KeyCode] then
			keycodeToGui[input.KeyCode].Enabled = false
		end
	end)
end

checkKeys()

Regarding your second question, as long as the GUIs have ResetOnSpawn = false that is fine. Otherwise it will break after you respawn once, since the script will be tied to the old versions that got discarded.

Local function checkkeys()

end

Jajaja yes is correct, is a fact

1 Like

Bro thank you so much, this works perfectly :heart:
I’ll keep it running in the background for a few minutes to see if a memory leak happens, but I don’t think there will be.

As for the second question I had in this post, Do you know if its possible to have a gui that activates only when someone is in shiftlock? Every post I see is about using LockCenter, but that activates when a player is in shiftlock and in first person.

Off the top of my head, I don’t really think there’s a good way outside of LockCenter, but I’ll spitball some potential solutions

  1. Check for shift being pressed while MouseLockOption is enabled (see the following article)
  1. You could try to detect the camera offset (Workspace.CurrentCamera), since shiftlock will cause the camera to be offset and not directly focus on the Character

  2. You could try to detecting the LockCenter change, but also check if the player is fully zoomed in. If they’re fully zoomed in, then shiftlock is probably not on, otherwise it probably is on.

Alright I’ll definitely try that, although I have to get off right now. Thanks for the massive help anyways

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