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

What to do with my sprint script because I am confused why it is not working?

Asked by 6 years ago

Basically, I am making a script where you have to press Q on the keyboard and the player will get faster but for some reason it is not working. When i run it a press q is says this:

13:46:19.046 - WalkSpeed is not a valid member of Model

so is it saying the walkspeed is not a value in the humanoid model

local humanoid = game.Players.LocalPlayer
sprinting = true
game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        humanoid.Character.WalkSpeed = 40
        end
end)

1 answer

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

On line 5, you are indexing the WalkSpeed property from the character model. WalkSpeed is a property of Humanoid. Instead, you should say:

local player = game.Players.LocalPlayer -- player or localPlayer are more accurate names for this variable
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local sprinting = true
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        humanoid.WalkSpeed = 40
    end
end)

Notice how I used variables to keep things organized, and also how i used Connect rather than connect, because the lowercase edition is deprecated.

Hope this helps.

0
On line 2, it should be this: "local character = player.Character or player.CharacterAdded:Wait()". Scripts load before the character is added, so it's a good idea to yield a bit until character loads. Apart from that, good answer. +1 upvote, again :) . User#19524 175 — 6y
0
That's good to know, I figured that in local scripts I would also be able to reference the character of the player immediately. Thanks! :) chomboghai 2044 — 6y
0
Thank you guys for taking the time to assist me with my problem. IFeelLikeAGucciAdlib -2 — 6y
Ad

Answer this question