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

Why won't this shift to sprint script function?

In my game there is a shift to sprint script however for some reason it won't function and I can't identify the problem. Any help would be greatly appreciated! :)

Here is the script:

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:wait()

local Mouse = Player:GetMouse()
local Speed = 33
local Humanoid = Player.Character:WaitForChild("Humanoid")
local OrigSpeed = Humanoid.WalkSpeed
local active = false

UserInputService.InputBegan:Connect(function(input)
local key = input.KeyCode
    if key == Enum.KeyCode.LeftShift and active == false then
        Humanoid.WalkSpeed = Speed
        active = true
    elseif key == Enum.KeyCode.LeftShift and active == true then
        Humanoid.WalkSpeed = OrigSpeed
        active = false
    end
end) 

2 answers

First, the error. I believe that the line 3 Player.CharacterAdded:Wait() is kind of breaking your script because it yields your script until character is actually added. What I mean is that if your character is already loaded, the CharacterAdded event won`t fire and that script will have to wait forever. To fix that, you can do this so it will check if character is loaded, and if not it will wait.

local character = Player.Character or Player.CharacterAdded:Wait()

Also, I think you should use ContextActionService instead of UserInputService because it is making you code a bit more complicated than it should be. To use ContextActionService, you can do

local function sprint(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		--Your Sprint Code
	elseif (inputState == Enum.UserInputState.End) then
		--Make your speed to normal speed
	end
end

game:GetService("ContextActionService"):BindAction("Sprint"--[[Name of action--]],sprint--[[function--]],false--[[if you want to make a mobile button for this action, make it true--]],Enum.KeyCode.LeftShift--[[when to fire--]])	

I hope this helps :D

you can have this script

function onPlayerEntered(player)
	repeat wait () until player.Character ~= nil
	local s = script.SprintScript:clone()
	s.Parent = player.Character
	s.Disabled = false
	player.CharacterAdded:connect(function (char)
		local s = script.SprintScript:clone()
		s.Parent = char
		s.Disabled = false		
	end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

then do a `local script`

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end

then do a local script and put it inside the script

mouse.KeyDown:connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)


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