So i am making thi sprint script that if you press w you automatically sprint. However when i equip or unequip it inverts for some reason. I’ve tried disconnecting the inputended but that didn’t really work out.
Script:
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local rS = game:GetService("ReplicatedStorage")
local tS = game:GetService("TweenService")
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local isSprinting = false
local SprintAnim = rS:WaitForChild("Animations"):WaitForChild("Sword"):WaitForChild("Main"):WaitForChild("Sprint")
local sprintTrack = humanoid.Animator:LoadAnimation(SprintAnim)
local sprintSpeed = 20
local function HandleSprinting()
if isSprinting then
humanoid.WalkSpeed = 16
tS:Create(camera, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()
isSprinting = false
sprintTrack:Stop()
char:SetAttribute("Sprinting", false)
elseif not isSprinting then
char:SetAttribute("Sprinting", true)
humanoid.WalkSpeed = sprintSpeed
tS:Create(camera, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80}):Play()
isSprinting = true
sprintTrack:Play()
end
end
local function onKeyPressed(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W and not char:GetAttribute("Stunned") and not char:GetAttribute("IsEquipping") and not char:GetAttribute("IsBlocking") and not char:GetAttribute("IsRagdoll") then
HandleSprinting()
end
end
local function onKeyRelease(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W and not char:GetAttribute("Stunned") and not char:GetAttribute("IsEquipping") and not char:GetAttribute("IsBlocking") and not char:GetAttribute("IsRagdoll") then
HandleSprinting()
end
end
UIS.InputBegan:Connect(onKeyPressed)
UIS.InputEnded:Connect(onKeyRelease)
humanoid.Changed:Connect(function()
local stunned = char:GetAttribute("Stunned")
local isRagdoll = char:GetAttribute("IsRagdoll")
if stunned or isRagdoll or char.HumanoidRootPart.Anchored then
if isSprinting then HandleSprinting() end
end
end)
char.ChildAdded:Connect(function()
if char:FindFirstChild("Katana") then
if isSprinting then HandleSprinting() end
end
end)
char.ChildRemoved:Connect(function()
if not char:FindFirstChild("Katana") then
if isSprinting then HandleSprinting() end
end
end)
Video of what is happening (de video is from guilded.gg):
If the Katana is a Tool, then I recommend parenting the LocalScript to it and using its Equipped and Unequipped events to keep track of when the character should be able to sprint
@famousvilliger1 This should work to achieve what you’re looking for, although as previously mentioned it will need to be a LocalScript that’s a direct child of the Katana Tool in-order to be able to work:
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local TWEEN_INFO = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local camera = Workspace.CurrentCamera or Workspace:WaitForChild("Camera")
local katana = script.Parent
local character
local humanoid
local sprintTrack = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Sword"):WaitForChild("Main"):WaitForChild("Sprint")
local function onKatanaInput(_, inputState)
if
character:GetAttribute("Stunned")
or character:GetAttribute("IsBlocking")
or character:GetAttribute("IsRagdoll")
then
return
end
if inputState == Enum.UserInputState.Begin then
humanoid.WalkSpeed = 20
TweenService:Create(camera, TWEEN_INFO, {FieldOfView = 80}):Play()
sprintTrack:Play()
else
humanoid.WalkSpeed = 16
TweenService:Create(camera, TWEEN_INFO, {FieldOfView = 70}):Play()
sprintTrack:Stop()
end
return Enum.ContextActionResult.Pass
end
katana.Equipped:Connect(function()
if not character then
character = katana.Parent
humanoid = character.Humanoid
sprintTrack = humanoid.Animator:LoadAnimation(sprintTrack)
end
ContextActionService:BindAction("KatanaInput", onKatanaInput, false, Enum.KeyCode.W)
end)
katana.Unequipped:Connect(function()
ContextActionService:UnbindAction("KatanaInput")
onKatanaInput(nil, Enum.UserInputState.End)
end)
BTW it’s hard to tell that the walk-speed has changed by setting it to 20. I left it at that value since it’s what you used in the original code, but I recommend increasing it to something like 32
This could work, but I forgot to say that the same thing happens when unblocking and blocking. So when i press W key while blocking and stop blocking and release it. Also when getting attacked and then releasing the w key
That issue might have been caused with how the if statements were checking the attributes in the original code, hopefully the issue won’t show up now since the if statements are handled differently in the new version (now exiting from the function should be guaranteed if one of the attributes is true)
The script gives unfortunately gives alot of bugs like I can’t walk forward while blocking and it doesnt sprint when katana is unequipped. Also when i stop blocking it keeps the animation playing and walks by its own. Maybe you could do something with my sprinting script? (btw I changed runspeed to 25)
Fixed it by just checking if he is sprinting when releasing the w key
local function onKeyPressed(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W and not char:GetAttribute("Sprinting") and not char:GetAttribute("Stunned") and not char:GetAttribute("IsEquipping") and not char:GetAttribute("IsBlocking") and not char:GetAttribute("IsRagdoll") then
HandleSprinting()
end
end
local function onKeyRelease(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.W and char:GetAttribute("Sprinting") and not char:GetAttribute("Stunned") and not char:GetAttribute("IsEquipping") and not char:GetAttribute("IsBlocking") and not char:GetAttribute("IsRagdoll") then
HandleSprinting()
end
end
The script I wrote is intended to be used while the katana is equipped, and usually a different keycode is used for sprinting like for example LeftShift rather than the W key since it can interfere with the character’s movement. If the character is allowed to sprint even if the katana is unequipped, then why are you checking to see if it is in the original code?