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

There is this error that I cant fix how do I fix it? I'm trying to make a sprint script.

Error: 14:14:51.507 Players.CMB1048.PlayerScripts.LocalScript:22: Expected 'end' (to close 'then' at line 13), got ; did you forget to close 'then' at line 18? - Studio - LocalScript:22 btw sorry i had to repeat this

UserInputService = game:GetService("UserInputService")
local players = game.Players.LocalPlayer



local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

UserInputService.InputBegan:Connect(function(input,gameProcessed)
	--if not gameProcessed then
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if players:FindFirstChild("Humanoid") then
			players.Humanoid.CharacterWalkSpeed = 90
			
			UserInputService.InputEnded:Connect(function(input,gameProcessed)
				if input.KeyCode == Enum.KeyCode.LeftShift then
					if players:FindFirstChild("Humanoid") then
						players.Humanoid.CharacterWalkSpeed = 16
					end
				end
			end)

2 answers

Try this

Add a LocalScript to StarterCharacterScripts

Use this code

local userInputService = game:GetService("UserInputService")
local humanoid = script.Parent:FindFirstChild("Humanoid")

local sprinting = false

userInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		
		if sprinting == false then
			sprinting = true
			
			humanoid.WalkSpeed = 32
			
		else
			
			sprinting = false
			
			humanoid.WalkSpeed = 16
			
		end
		
	end
end)

2 comments

0

Did not work

0

wait it workes sorry

Whenever the error mentions "", it's because you forgot an end somewhere (or, in this case, three of them). If you keep up consistent indentation, it makes it easier to find - whenever you have a double-unindent, there's a problem.

Also, you should almost never connect to events inside other events!

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 90
    end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 16
    end
end)

Modifications:

  • Reference humanoid once outside of the events for simplicity (I assume this is a StarterCharacterScript)
  • Respect gameProcessed (or else when the user is chatting and presses shift or other controls you'll process them instead of correctly ignoring them)
  • InputEnded is connected to once, not each time the user presses a key
  • It's WalkSpeed not CharacterWalkSpeed

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