Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

3 comments

0

It appears your char variable is not pointing towards the Character model, but just the player.

0

That has been fixed.

0

Sorry I should of mentioned I do have a custom character, it's a standard R15 character (not a custom model).

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)

2 comments

0

This doesn't seem to work, could it be because I have a Custom Model

0

well, that shouldn't cause a problem as far as i know

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.

3 comments

0

Walkspeed should be done on the client, not on the server.

0

This didn't work, could it be because I have a custom character?

0

After looking around, it appears Thesquid13 is correct. Also, if you made any changes, I suggest updating your post.

This discussion is preserved as it was. New answers, comments, and votes are closed.