local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = nil
local Humanoid = nil
local myEvent = game.ReplicatedStorage:WaitForChild("Event")
local cansprint = true
-- Function to update character references
local function setupCharacter(newCharacter)
Character = newCharacter
Humanoid = Character:WaitForChild("Humanoid")
end
-- Set up initial character
if Player.Character then
setupCharacter(Player.Character)
end
-- Connect to CharacterAdded to update references when player respawns
Player.CharacterAdded:Connect(setupCharacter)
local function onInput(input)
if not Humanoid then return end -- Guard clause to prevent errors
if input.KeyCode == Enum.KeyCode.LeftShift and cansprint == true then
Humanoid.WalkSpeed = 25
elseif input.UserInputType == Enum.UserInputType.Keyboard then
print("User pressed a key on the keyboard:", input.KeyCode)
end
end
local function EndInput(input)
if not Humanoid then return end -- Guard clause to prevent errors
if input.KeyCode == Enum.KeyCode.LeftShift and cansprint == true then
Humanoid.WalkSpeed = 16
end
end
UserInputService.InputBegan:Connect(onInput)
UserInputService.InputEnded:Connect(EndInput)
myEvent.Event:Connect(function()
if UserInputService.InputBegan then
print("Event Fired")
Humanoid.WalkSpeed = 0
print("set to 0")
local cansprint = false
print("var set to false")
task.wait(5)
Humanoid.WalkSpeed = 16
local cansprint = true
print("set to true")
end
end)
So this was a code for sprinting that i made. I made a seperate script taht would play an animation, and I wanted to make sure people couldn't walk during this. The problem is that it works until you press left shift, and after you do so it would let you walk around. I tried fixing this with a variable and adding "and" to my if conditions but it didnt work,