Ad
Log in to vote
0

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

Asked by
CMB1048 23
1 year ago

Error: 14:14:51.507 Players.CMB1048.PlayerScripts.LocalScript:22: Expected 'end' (to close 'then' at line 13), got <eof>; 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

Log in to vote
1
Answered by
MattVSNNL 614 Moderation Voter
1 year ago

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)
0
Did not work CMB1048 23 — 1y
0
wait it workes sorry CMB1048 23 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Whenever the error mentions "<eof>", 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

Answer this question