Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cannot make player sprint when a tool is equipped?

Asked by 3 years ago

I can't seem to make players in my game sprint when they have a sword equipped, specifically when they press "E". I'll post the code that I have below, any help would be greatly appreciated!

sword = game.StarterPack.ClassicSword

sword.Equipped:Connect(function()
    local player = game.Players.LocalPlayer
    local character = player.Character

    local UserInputService = game:GetService("UserInputService")

    local eHeld = UserInputService:IsKeyDown(Enum.KeyCode.E)

    if eHeld == true then

    character.Humanoid.WalkSpeed = 20

    elseif eHeld == false then
        character.Humanoid.WalkSpeed = 16

    end

end)
0
Get the backpack of the localplayer not starterpack, I'll make an answer for it right now Pupppy44 671 — 3y

1 answer

Log in to vote
0
Answered by
Pupppy44 671 Moderation Voter
3 years ago

Line 1,

sword = game.StarterPack.ClassicSword

You're trying to get the sword in the StarterPack, not the local player's backpack. Starterpack is for giving a player tools every time their character loads. Fixed script:

local sword = game.Players.LocalPlayer.Backpack:WaitForChild('ClassicSword') -- here

sword.Equipped:Connect(function()
    local player = game.Players.LocalPlayer
    local character = player.Character

    local UserInputService = game:GetService("UserInputService")

    local eHeld = UserInputService:IsKeyDown(Enum.KeyCode.E)

    if eHeld == true then

    character.Humanoid.WalkSpeed = 20

    elseif eHeld == false then
        character.Humanoid.WalkSpeed = 16

    end

end)

Hope this helps

Ad

Answer this question