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

LeftShift to sprint script doesn't work ?

I made a little script that should change the player's walkspeed to 50 when LeftShift is pressed but it didn't work.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == Enum.KeyCode.LeftShift then
		local Human = script.Parent:WaitForChild("Humanoid")
		Human.Walkspeed = 50
end
	if not key then
	 	local Human = script.Parent:WaitForChild("Humanoid")
		Human.Walkspeed = 16
	end
end)

2 answers

✓ Accepted answer

This already won't work because you're using 'KeyDown' wich is deprecated and will not work anymore, use userinputservice instead.

Not sure where your script is but most of the time player isn't the parent, (mostly playergui or backpack)

Also, you're thinking humanoid is located inside the player wich is not the case, it's located inside the character

So a better script would be

local UserInputService = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer

function onInputBegan(input,gameProcessed)
        if input.KeyCode == Enum.KeyCode.LeftShift then
 		local char = Player.Character
		if char then
			local h = char:WaitForChild('Humanoid')
			h.WalkSpeed = 50
		end
	end
end

function onInputEnded(input,gameProcessed)
     if input.KeyCode == Enum.KeyCode.LeftShift then
 		local char = Player.Character
		if char then
			local h = char:WaitForChild('Humanoid')
			h.WalkSpeed = 16
		end
	end
end


UserInputService.InputBegan:connect(onInputBegan)
UserInputService.InputEnded:connect(onInputEnded)

This needs to be a local script in any client area.

If you have any questions, just ask :)

2 comments

0

Thanks ! Worked

0

Np :)

Right, so a mouse. Does it have keys? No a keyboard does. So, instead of using the mouse. You should use something called UserInputService.

So, just do..

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key_) -- Why the underscore? Because thats the keycode of the key.
	local key = key_.KeyCode
	if key == Enum.KeyCode.YourKeyHere then
		--code
	end
end)

1 comment

0

He was using keydown lol

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