I am trying to run a little shift to sprint code and as i started coding i went to test the first bit of code and it outputs nothing not even the print
Here’s the code
local input = game:GetService(“UserInputService”)
local keydown = input:IsKeyDown(Enum.KeyCode.LeftShift)
local function inputgiven()
if keydown then
print(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 45
end
end
input.InputBegan:Connect(inputgiven)
I know its not a full shift to sprint but why is nothing working at all?
1 Like
This is a better code:
local input = game:GetService(“UserInputService”)
local function inputgiven(Key,IRP)
if IRP then return end
if Key.KeyCode == Enum.KeyCode.LeftShift then
wait(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 45
end
end
local function inputended(KeyCode,IRP)
if IRP then return end
if KeyCode == Enum.KeyCode.LeftShift then
wait(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
end
end
end
input.InputEnded:Connect(inputended)
input.InputBegan:Connect(inputgiven)
if you get a error, say.
2 Likes
I believe when the script detects if the key is down at the start it returns false hence setting the value of the variable to false. After that when input is given, it doesn’t change the value and so the if statement doesn’t work.
Try using a combination of inputbegan and inputended to set and unset the variable each time it’s pressed / released.
He said it’s not a full script so that’s not the issue rn.
i understand i haven’t tried inputting that yet because i was having issues
i tried moving the keydown inside the function but then it outputs that keydown isnt a valid member
Could you show me the updated code?
local input = game:GetService(“UserInputService”)
local function inputgiven(input,gameProcessedEvent)
local keydown = input:IsKeyDown(Enum.KeyCode.LeftShift)
if keydown then
print(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 45
end
end
input.InputBegan:Connect(inputgiven)
You are overriding the initial input variable with the parameter.
1 Like
so how would i reformat this script then?
Change the “input” parameter to “input1” or something.
after some tweaking i fixed the code and made it fully functional, thanks for the help friend!
local input1 = game:GetService(“UserInputService”)
local function keydown()
return input1:IsKeyDown(Enum.KeyCode.LeftShift)
end
local function inputgiven(input,gameProcessedEvent)
if not keydown() then
print(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
else
print(2)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
end
end
local function inputend(input2,gameProcessedEvent)
if not keydown() then
print(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
else
print(2)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
end
end
input1.InputBegan:Connect(inputgiven)
input1.InputEnded:Connect(inputend)
2 Likes
why did you put in the function input and gameProccesedEvent even you don’t use it?
More of a precaution to save me time in future if i ever need it