#Variable

36 messages · Page 1 of 1 (latest)

shadow skiff
#

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,

leaden apex
shadow skiff
#

the script is a sprint toggle

#

but i have this thing where if you press a button, an animation is played

#

i wanted to ensure you couldn't walk during this animation

leaden apex
shadow skiff
leaden apex
#

okay so just disable sprint toggling during the animation and then set the player's character's humaoid's walkspeed a jump power to 0 temporarily

#

make sure your animation is Enum.Action4 too (highest priority)

leaden apex
#

do which part

shadow skiff
leaden apex
#

okay regardless just do what I said, Humanoid.WalkSpeed = 0, Humanoid.JumpPower = 0

#

wont be able to move even if theyre inputting

shadow skiff
leaden apex
#

because you're setting their walkspeed up/down

#

you need to make it so they can't sprint toggle during the cutscene

shadow skiff
#

idk how else to do it

leaden apex
#

just set it to false, then send another signal when the cutscene is over, and set it to true

shadow skiff
#

still doesnt work

#
        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)```
#

these were my last few lines of code

#

still didnt work

#

the print lines were me trying to debug

leaden apex
#

the event itself is always going to be true

#

you would need to do UserInputService.InputBegan:Connect(function()
code
end)

#

but you already also have an InputBegan connection, so why make a new one?

shadow skiff
shadow skiff
#

i changed it to that since i needed the event but not the input began thing

leaden apex
#

yah that should work