I want to allow the LocalPlayer to crouch when they press a hotkey out of 3 hotkeys (C, LeftControl, RightControl). But, I don’t want the player to be able to crouch while chatting.
Currently, they player can crouch with the hotkey, even while typing in chat. How do I prevent this?
LocalScript in StarterGui:
-- Hotkeys
local Hotkey = "C"
local Hotkey2 = "LeftControl"
local Hotkey3 = "RightControl"
-- Services
local PlayerSerivce = game:GetService("Players")
local TweenService = game:GetService("TweenService")
-- User Related References
local Player = PlayerSerivce.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
-- Animation
local CharacterAnimation
local Crawling = false
CharacterAnimation = Character.Humanoid:LoadAnimation(game:GetService("ReplicatedStorage"):WaitForChild("Crawl"))
-- Functions
function ChangeCameraOffset(Offset)
if not Character then Character = Player.Character or Player.CharacterAdded:wait() end
local Humanoid = Character:WaitForChild("Humanoid")
local Tween = TweenService:Create(Humanoid, TweenInfo.new(0.25), {CameraOffset = Offset})
Tween:Play()
end
local function Animate()
if Player.PlayerGui.IsMonster.Transformed.Value == false then
print("crawl")
Humanoid.WalkSpeed = 8 -- WalkSpeed Change
Humanoid.JumpPower = 0
ChangeCameraOffset(Vector3.new(0, -3, 0))
Humanoid.MaxSlopeAngle = 50
if not Crawling then
CharacterAnimation:Play()
CharacterAnimation:AdjustSpeed(0)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
Crawling = true
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
CharacterAnimation:AdjustSpeed(1)
else
CharacterAnimation:AdjustSpeed(0)
end
end)
elseif Crawling then
Humanoid.JumpPower = 50
Crawling = false
Humanoid.WalkSpeed = 16 -- WalkSpeed Change
ChangeCameraOffset(Vector3.new(0, 0, 0))
Humanoid.MaxSlopeAngle = 50
CharacterAnimation:Stop()
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
else
print("dont crawl")
end
end
-- Main
game:GetService("UserInputService").InputBegan:Connect(function(InputObject)
if InputObject.KeyCode == Enum.KeyCode[Hotkey] or InputObject.KeyCode == Enum.KeyCode[Hotkey2] or InputObject.KeyCode == Enum.KeyCode[Hotkey3] then
Animate()
end
end)
-- Hotkeys
local Hotkey = "C"
local Hotkey2 = "LeftControl"
local Hotkey3 = "RightControl"
-- Services
local PlayerSerivce = game:GetService("Players")
local TweenService = game:GetService("TweenService")
-- User Related References
local Player = PlayerSerivce.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
-- Animation
local CharacterAnimation
local Crawling = false
CharacterAnimation = Character.Humanoid:LoadAnimation(game:GetService("ReplicatedStorage"):WaitForChild("Crawl"))
-- Functions
function ChangeCameraOffset(Offset)
if not Character then Character = Player.Character or Player.CharacterAdded:wait() end
local Humanoid = Character:WaitForChild("Humanoid")
local Tween = TweenService:Create(Humanoid, TweenInfo.new(0.25), {CameraOffset = Offset})
Tween:Play()
end
local function Animate()
if Player.PlayerGui.IsMonster.Transformed.Value == false then
print("crawl")
Humanoid.WalkSpeed = 8 -- WalkSpeed Change
Humanoid.JumpPower = 0
ChangeCameraOffset(Vector3.new(0, -3, 0))
Humanoid.MaxSlopeAngle = 50
if not Crawling then
CharacterAnimation:Play()
CharacterAnimation:AdjustSpeed(0)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
Crawling = true
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
CharacterAnimation:AdjustSpeed(1)
else
CharacterAnimation:AdjustSpeed(0)
end
end)
elseif Crawling then
Humanoid.JumpPower = 50
Crawling = false
Humanoid.WalkSpeed = 16 -- WalkSpeed Change
ChangeCameraOffset(Vector3.new(0, 0, 0))
Humanoid.MaxSlopeAngle = 50
CharacterAnimation:Stop()
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
end
else
print("dont crawl")
end
end
-- Main
game:GetService("UserInputService").InputBegan:Connect(function(InputObject, GameProcessedEvent)
if GameProcessedEvent then return end
if InputObject.KeyCode == Enum.KeyCode[Hotkey] or InputObject.KeyCode == Enum.KeyCode[Hotkey2] or InputObject.KeyCode == Enum.KeyCode[Hotkey3] then
Animate()
end
end)
Adding to what @domboss37 has said,
Whenever you use UserInputService and InputBegan, the second parameter would help you to prevent such cases where someone presses a key and checks if there is a gui open. Useful parameter in these cases.