Below is a chunk of code for some gameplay animations I have been working on. They work as intended aside from the line that is intended to disable the Backpack GUI.
I am not sure why this isn’t disabling my backpack. There are other functions in this same script that are set up essentially the same way and properly disable the backpack GUI.
That being said, also note that this script is a LocalScript that is a child of StarterGui.
m.KeyDown:Connect(function(Key, typing)
if typing then return end
local Humanoid = player.Character:FindFirstChild('Humanoid')
local currState = Humanoid:GetState()
if Key == "c" and Humanoid.WalkSpeed >= tonumber(WalkingSpeed) and currState == Enum.HumanoidStateType.Running then
IsCrouching = true
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
player.Character.Humanoid:UnequipTools() -- unequips all equipped tools.
if InputService:IsKeyDown(Enum.KeyCode.W) or InputService:IsKeyDown(Enum.KeyCode.A) or InputService:IsKeyDown(Enum.KeyCode.S) or InputService:IsKeyDown(Enum.KeyCode.D) or InputService:IsKeyDown(Enum.KeyCode.Up) or InputService:IsKeyDown(Enum.KeyCode.Down) then
Animate = Humanoid:LoadAnimation(CrouchWalkAnim)
Animate:Play()
else
Animate = Humanoid:LoadAnimation(CrouchAnim)
Animate:Play()
end
end
m.KeyDown:Connect(function(Key)
local numberCode = tonumber(string.byte(Key)); -- For up and down arrow keys
if (Key == "w" or Key == "a" or Key == "s" or Key == "d" or numberCode == 17 or numberCode == 18) and IsCrouching then
Animate:Stop()
Animate = Humanoid:LoadAnimation(CrouchWalkAnim)
Animate:Play()
end
end)
m.KeyUp:Connect(function(Key)
local numberCode = tonumber(string.byte(Key)); -- For up and down arrow keys
if (Key == "w" or Key == "a" or Key == "s" or Key == "d" or numberCode == 17 or numberCode == 18) and IsCrouching and not(InputService:IsKeyDown(Enum.KeyCode.W) or InputService:IsKeyDown(Enum.KeyCode.A) or InputService:IsKeyDown(Enum.KeyCode.S) or InputService:IsKeyDown(Enum.KeyCode.D) or InputService:IsKeyDown(Enum.KeyCode.Up) or InputService:IsKeyDown(Enum.KeyCode.Down)) then
Animate:Stop()
Animate = Humanoid:LoadAnimation(CrouchAnim)
Animate:Play()
end
end)
end)
m.KeyUp:Connect(function(Key)
if Key == "c" and IsCrouching == true then
IsCrouching = false
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
Animate:Stop()
end
end)
I am inside of a local script that can run that same line in several other locations just fine. It is only in this function that it decides not to work.
Probably because the core scripts and GUI of the Backpack system are not yet registered to the StarterGui service. Instead, refer to this post’s solution:
Edit: Realized that if the if statements were the issue, then the animations and other things in your script wouldn’t run. Keeping this post up for future reference.
It’s possible that the line of code is just not running because it’s getting stopped by the if statements.
Since I have no clue about what the rest of your script is trying to do, or how it works, I just made my own code to make sure that your line of code that disables the backpack should work, if it were to run. My example below does disable backpack when pressing the c key:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
UserInputService.InputBegan:Connect(function(input, gp)
if not gp and Enum.UserInputType.Keyboard == input.UserInputType then
local keyCode = input.KeyCode
local key = UserInputService:GetStringForKeyCode(keyCode)
if string.lower(key) == "c" then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
end
end
end)
UserInputService.InputEnded:Connect(function(input, gp)
if not gp and Enum.UserInputType.Keyboard == input.UserInputType then
local keyCode = input.KeyCode
local key = UserInputService:GetStringForKeyCode(keyCode)
if string.lower(key) == "c" then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
end
end
end)
The fact remains is that it would work if that line in your code would actually run. try adding print statements inside the if statements to see where the code stops running.
Btw, I found my own problem, if somebody is still trying to find the errors of the SetCoreGuiEnabled its probably the spellings sometimes errors like this are annoying