What do you want to achieve? Keep it simple and clear!
I want soo when the player press LeftControl the player crouches
What is the issue? Include screenshots / videos if possible!
It works just the first time, soo when i press LeftControl it crouches but when i press LeftControl again it doesnt get up and i stay crouched until i reset
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I honestly dont know what to do since i dont find nothing wrong in my script, also i dont get any error in the output
This is the code wich is a local script in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local plrs = game:GetService("Players")
local db = false
local crouch = false
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftControl then
if not db then
db = true
local player = plrs:GetPlayerFromCharacter(script.Parent)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not crouch then
crouch = true
if humanoid then
humanoid.HipHeight = -1.5
humanoid.WalkSpeed = 6
end
end
if crouch then
crouch = false
if humanoid then
humanoid.HipHeight = 0
humanoid.WalkSpeed = 16
end
end
task.wait(2)
db = false
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)
this should work for you, but i would recommend just making a crouch animation as changing hipheight is a bit janky
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local db = false
local crouch = false
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftControl then
if not db then
db = true
if not crouch then
crouch = true
print("crouching")
humanoid.HipHeight = -1.5
humanoid.WalkSpeed = 6
else
crouch = false
print("not crouching")
humanoid.HipHeight = 0
humanoid.WalkSpeed = 16
end
task.wait(2)
db = false
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)
Also, this was the problem, you were changing it to true then immediately checking it and toggling again, in a different if statement. You would need to use else or elseif instead.
local UserInputService = game:GetService("UserInputService")
local plrs = game:GetService("Players")
local player = plrs.LocalPlayer
local db = false
local crouch = false
local defaultHipHeight = 2
local function onInputBegan(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftControl then
if not db then db = true
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
if not crouch then
crouch = true
humanoid.HipHeight = 1
humanoid.WalkSpeed = 6
else crouch = false
humanoid.HipHeight = defaultHipHeight
humanoid.WalkSpeed = 16
end
end task.wait(1)
db = false
end
end
end
UserInputService.InputBegan:Connect(onInputBegan)