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

This script should make the character walk faster but it doesn't. Why?

Asked by 5 years ago

I'm trying to make a character sprint when you press left shift, however when I test this my character stays at normal speed. (Also sorry if the answer is super obvious I just started trying to code yesterday)

local uis = game:GetService("UserInputService")
local char = game.StarterPlayer.StarterCharacter

uis.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        char.Humanoid.WalkSpeed = 30
    end
end)

Thanks in advance

3 answers

Log in to vote
0
Answered by 5 years ago

You don't appear to be doing anything that would normally wrong except for you not accounting for Experimental Mode is off, meaning anything done on client does not necessarily reach the server. I suggest you read up on Remotes so you can properly modify your script.

**First, I hope you are using a local script, if so please change line 2 into local char = game.Player.LocalPlayer.CharacterAdded:wait() **

I suggest using a RemoteEvent, on line 6 you should replace your code with: RemoteEvent:FireServer() Due to your desire to sprint, you don't need to pass any arguments to pick a speed because it should be a set amount you can change on the server.

Now you need a server script with the following code:

RemoteEvent.OnServerEvent:Connect(function(Player)
    if Player.Character.Humanoid.Health > 0 then
        Player.Character.Humanoid.WalkSpeed = 30
    end
end

All you need to do after this point is to be sure you create a way to slow your character down when they are done sprinting and to make RemoteEvent a variable or a directory to the RemoteEvent you are using.

0
Walkspeed should be done on the client, not on the server. Thesquid13 301 — 5y
0
This didn't work, could it be because I have a custom character? Choco73887 2 — 5y
0
After looking around, it appears Thesquid13 is correct. Also, if you made any changes, I suggest updating your post. alphawolvess 1784 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

If you aren't using a custom character model, this isn't the correct way to do it. This is how I would do it, assuming you aren't using a custom character.

local uis,char=game:GetService("UserInputService"),game:GetService("Players").LocalPlayer.Character

uis.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        char.Humanoid.WalkSpeed = 30
    end
end
0
It appears your char variable is not pointing towards the Character model, but just the player. alphawolvess 1784 — 5y
0
That has been fixed. Control22 5 — 5y
0
Sorry I should of mentioned I do have a custom character, it's a standard R15 character (not a custom model). Choco73887 2 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

your code looks good, the one possible cause of this could be putting the code in server script; so if the code is in server script then put it in LocalScript and parent the LocalScript in places like starter pack,or etc. just not in any server related services like - workspace,server script service, server storage, and etc.

** so example of how you could do it in local script:**

local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local character = player.Character
uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if character then
            character:WaitForChild("Humanoid").WalkSpeed = 30
        end
    end
end)
0
This doesn't seem to work, could it be because I have a Custom Model Choco73887 2 — 5y
0
well, that shouldn't cause a problem as far as i know User#23252 26 — 5y

Answer this question