Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

So... Here is my sprint script it is somehow working buti feel like that it can be better?

Asked by 6 years ago
Edited 6 years ago
01local mouse = game.Players.LocalPlayer:GetMouse()
02local running = false
03 
04function getTool() 
05    for _, kid in ipairs(script.Parent:GetChildren()) do
06        if kid.className == "Tool" then return kid end
07    end
08    return nil
09end
10 
11 
12mouse.KeyDown:connect(function (key) -- Run function
13    key = string.lower(key)
14    if string.byte(key) == 48 then
15        running = true
View all 42 lines...

It sprints based on player's agility and drains the player's stamina by 2 every 1/10th of a second(second block of code)

Experimented until it worked, but I have a strong feeling that there will be a bug somewhere and it can be improved

0
You shouldn’t be using KeyDown. User#19524 175 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

There won’t be a bug, but you shouldn’t be using KeyDown to get user input. It’s deprecated and just so inefficient. Instead, use UserInputService.InputBegan.

01local userInputService = game:GetService"UserInputService"
02local plr = game:GetService"Players".LocalPlayer
03 
04userInputService.InputBegan:Connect(function(key, chatting)
05    if chatting then
06        return
07    end
08 
09    if key.KeyCode == Enum.KeyCode.LeftShift then
10        plr.Character.Humanoid.WalkSpeed = 24 -- set to desired speed
11    end
12end)
13 
14userInputService.InputEnded:Connect(function(key, chatting)
15    if chatting then
View all 22 lines...
0
Should be InputEnded for the second one. mattscy 3725 — 6y
0
forgot lol User#19524 175 — 6y
0
Thanks, modified the script a little bit to make it drain stamina and stop on stamina. Thanks xD xxXTimeXxx 101 — 6y
Ad

Answer this question