Recently I’ve been trying to make a built-in nohboard script for roblox. I was able to get the W, A, S, D, and space keys working perfectly thanks to previous help, but as soon as I started adding in the shift keys things started bugging out.
The issue I have is that whenever I let go of a key, the gui stays visible as shown here:
Does anyone know what I’m doing wrong here? The entire script is below:
local run = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local userInput = game:GetService("UserInputService")
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local camera = workspace.CurrentCamera
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"),
[Enum.KeyCode.LeftShift] = playerGui:WaitForChild("NohboardShift"),
[Enum.KeyCode.RightShift] = playerGui:WaitForChild("NohboardShift")
}
-- 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()
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)
run.RenderStepped:Connect(function()
local distance = (head.Position - camera.CFrame.Position).Magnitude
if keycodeToGui[input.KeyCode] then
if Enum.KeyCode.LeftShift or Enum.KeyCode.RightShift then
if distance < 0.55 then
playerGui.NohboardShift.Enabled = false
else
playerGui.NohboardShift.Enabled = true
end
else
keycodeToGui[input.KeyCode].Enabled = false
end
end
end)
end)
end
checkKeys()