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

can someone help me make a script to run then have a time limit to when you can run again?

I need to know how to make a run script with a time limit like, hold shift to run for 3 sec, then wait 5 sec to run again. this is what I have if you can build off of it.


--Script- X To Run

local x = script.XToRun:clone()
x.Disabled = false
x.Parent = game.StarterGui
script.Destroy()

--Local Script- XToRun

mouse = game.Players.LocalPlayer:GetMouse()

function run (key)
	if key == "x" then
		game.Players.LocalPlayer.Character.Humanoid.Walkspeed = 32
end
end

function stopRunning (key)
	if key == "x" then
		game.Players.LocalPlayer.Character.Humanoid.Walkspeed = 16
	end
end

repeat wait()until game.Players.LocalPlayer
mouse.KeyDown:connect (run)

mouse.KeyUp:connect(stopRunning)


2 comments

0

PLEASE use the code block to hold code

0

ok

1 answer

✓ Accepted answer

User input with your mouse is deprecated, refer to UserInputService.


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

local now = 0
local maxrun_time = 3
local cooldown = 5
local max_walkspeed = 32
local min_walkspeed = 16
local running = Instance.new("BoolValue") -- So we can use the Changed event


local function change_walkspeed(ws)
	humanoid.WalkSpeed = ws
end

running.Changed:connect(function()
	if running.Value then 
		change_walkspeed(max_walkspeed)
	else
		change_walkspeed(min_walkspeed)
	end
end)

input.InputBegan:connect(function(inputType)
	if inputType.KeyCode == Enum.KeyCode.LeftShift then 
		local last_down = tick() - now
		local can_run = last_down > cooldown
		-- if the last time we tried to run was more than 5 seconds ago
		if can_run then 
			running.Value = true -- triggers Changed event above
			-- reset timer
			now = tick() 
			while running.Value do 
				local last_run = tick() - now
				if last_run < maxrun_time then 
					-- if we're currently running and we've been 
					-- running for less than the maxrun_time, do something
					print(string.format("%s can run for %s more seconds.", player.Name, tostring(maxrun_time - (last_run)):sub(1,3)))
				else
					break 
				end
				wait()
			end
			change_walkspeed(min_walkspeed)
		else
			print("waiting", last_down) 
		end
	end
end)

input.InputEnded:connect(function(inputType)
	if inputType.KeyCode == Enum.KeyCode.LeftShift and running.Value then 
		-- If we let shift go, reset the timer and stop us from running if we already are.
		now = tick()
		running.Value = false
	end
end)


3 comments

0

Would this go in a local script? and also where in the workspace would I put it?

0

Localscript, put it in StarterCharacterScripts

0

thank you so much

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