A while ago I was trying to make a script that would light up keys on a gui based on if the player was pressing those keys and if they were moving (using Enums and MoveDirection). It was a pretty simple function using task.wait(), but I think that’s where my issue was.
After creating the script, the game would lag tremendouly resulting in under 40FPS just by being in the game for a minute or two, and would continue to decrease after that.
I’m looking to see if there’s a different way to create this function. I need it to be super precise (practically activated on the frame) and I’m also looking to see if theres a way to detect if the player is in shift lock (just shift lock, LockCenter activates if the player is in both shift lock and first person). I wish I could send the script, but I created it months ago and deleted it as soon as I noticed the lag and gave up leaving no backups I could get. Does anyone know a potential function I could use to reduce the lag as well as creating a script that will only detect when a player is in shift lock?
Ok after a bit of digging, I was actually able to find the code. It looks like this:
local starterGui = game:GetService("StarterGui")
local userInput = game:GetService("UserInputService")
local nohA = script.Parent
nohA.Enabled = false
while task.wait() do
userInput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = true
end
end)
userInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = false
end
end)
end
Keep in mind that this is only for one of the keys… I’m not sure why I didn’t put them all in one script, but I think it would still be better to create some sort of garbage collector system and have a gui for shiftlock.
That is horrendous. Remove the while
loop and just convert the InputEnded to a :Wait() event.
I appreciate the honest feedback, I’m still pretty new to scripting so you shouldn’t expect much
I updated the script for a bit, and it looks something like this 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()
task.wait()
userInput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
nohW.Enabled = true
end
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = true
end
if input.KeyCode == Enum.KeyCode.S then
nohS.Enabled = true
end
if input.KeyCode == Enum.KeyCode.D then
nohD.Enabled = true
end
if input.KeyCode == Enum.KeyCode.Space then
nohSpace.Enabled = true
end
end)
userInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
nohW.Enabled = false
end
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = false
end
if input.KeyCode == Enum.KeyCode.S then
nohS.Enabled = false
end
if input.KeyCode == Enum.KeyCode.D then
nohD.Enabled = false
end
if input.KeyCode == Enum.KeyCode.Space then
nohSpace.Enabled = false
end
end)
end
The only problem is it doesn’t work at all now even though there are no errors in the script and everything autofilled perfectly. All of the GUIs stay on no matter if I’m pressing a key or not.
I’m also not sure what you meant by just having it be a :Wait() event, but I think this is what you mean?
First of all, I recommend that you use elseif instead of using several ifs, since it is not necessary to validate all the others if only one is true. I recommend showing the complete code because that way it would work well, I think.
I no unsterdant because You use Guis else of frames what is better
And the :wait no is necesary in this ocasion
I’m not sure if elseif would work because if the player were to press two keys at the same time, lets say W and D, I don’t think it would register both keys and would only show one or the other (from my understanding of elseif, but it could be wrong). That also is my entire code lol and its in StarterPlayerScripts, so if you don’t think it’s the entire code maybe I’m missing something?
No, is diferents events, límite of carácter nooooooo
what do you mean by this exactly?
You call they function? (Charcter limitttttttttt))))
I think so? I’m not too sure how functions work to be honest
I mean that when the player presses two keys, two different events are executed, that is, one event for each key since the input object can never return a table with two keys, it always returns the information of one key. Also you are not calling the function
In the end part
You code in the part end of the script, the function name and()
oh my god youre right lol im so dumb
so i do something like this?
task.wait(checkKeys())
Tranquilo, yo una vez pensé que si cambiaba cosas en el cliente los demás jugadores lo iban a ver.
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()
userInput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
nohW.Enabled = true
end
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = true
end
if input.KeyCode == Enum.KeyCode.S then
nohS.Enabled = true
end
if input.KeyCode == Enum.KeyCode.D then
nohD.Enabled = true
end
if input.KeyCode == Enum.KeyCode.Space then
nohSpace.Enabled = true
end
end)
userInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
nohW.Enabled = false
end
if input.KeyCode == Enum.KeyCode.A then
nohA.Enabled = false
end
if input.KeyCode == Enum.KeyCode.S then
nohS.Enabled = false
end
if input.KeyCode == Enum.KeyCode.D then
nohD.Enabled = false
end
if input.KeyCode == Enum.KeyCode.Space then
nohSpace.Enabled = false
end
end)
end
checkKeys()
Change the if for elseif remenber, good luck
So apparently the function of elseif is to “check for alternative conditions if the original one wasn’t met.” Does this mean that it won’t check for the other conditions if the original one is met?
1 Like