what do you want to achieve:
if the player sits and holds W a veriable goes up in steps of 1 slowly
What is the problem
I don’t know how
i tried it with this but it didn’t work and you have to continue pressing W
local CAS = game:GetService("ContextActionService")
local speed = script.Parent.Speed
local function Power()
if actionName == "SpeedUp" and inputState == Enum.UserInputState.Begin then
speed.Value +=5
end
end
if actionName == "SpeedDown" and inputState == Enum.UserInputState.Begin then
speed.Value -=5
end
CAS:BindAction("SpeedUp", Power, false, Enum.KeyCode.W)
CAS:BindAction("SpeedDown", Power, false, Enum.KeyCode.S)
local UIS = game:GetService("UserInputService")
local speed = script.Parent.Speed
local powerUp = true
local function Power(input, gpe)
if not gpe then
if Input.KeyCode == Enum.KeyCode.W then
while wait(1) do
if powerUp == false then
break
end
speed.Value +=5
end
end
if Input.KeyCode == Enum.KeyCode.S then
powerUp = false
end
end
end
local function powerDown(input, gpe)
if Input.KeyCode == Enum.KeyCode.W and not gpe then
powerUp = false
end
end
UIS.InputBegan:Connect(power)
UIS.InputEnded:Connect(powerDown)
They could press W repeteadly, while the while wait(1) do loop will wait for a second before checking
Player Press W
While wait runs
Player Repeats step 1 continuously for a second
Waits for 1 second
Script checks if player is holding W, if yes then while wait stops (Not All)
I think using run.Stepped is better
local run = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local speed = script.Parent.Speed
local Wheld = false
local interval = 1 -- Edit how fast it speeds up
local increment = 5 -- Edit how much speed gives
uis.InputBegan:Connect(function(key, gp)
if gp then return end
if key.KeyCode == Enum.KeyCode.W then
Wheld = true
end
end)
uis.InputEnded:Connect(function(key, gp)
if gp then return end
if key.KeyCode == Enum.KeyCode.W then
Wheld = false
end
end)
local timeSinceUpdate = tick()
run.Stepped:Connect(function()
if Wheld then
if (tick() - timeSinceUpdate) < interval then return end
speed.Value += increment
timeSinceUpdate = tick()
end
end)
This one increases speed if W is pressed after 1 second since the last time it increased. Meaning speed increases even though you are just spamming W.
If you want you can also make it that there is a delay, and only by holding the delay time, decreases
local run = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local speed = script.Parent.Speed
local Wheld = false
local interval = 1 -- Edit how fast it speeds up
local increment = 5 -- Edit how much speed gives
uis.InputBegan:Connect(function(key, gp)
if gp then return end
if key.KeyCode == Enum.KeyCode.W then
print("W key held")
Wheld = true
end
end)
uis.InputEnded:Connect(function(key, gp)
if gp then return end
if key.KeyCode == Enum.KeyCode.W then
print("W key released")
Wheld = false
end
end)
local timeSinceUpdate = tick()
run.Stepped:Connect(function()
if Wheld then
if (tick() - timeSinceUpdate) < interval then return end
speed.Value += increment
print("Speed Increased to", speed.Value)
timeSinceUpdate = tick()
end
end)
if it does not work, send me a screenshot or type what is written on the output
local vs = script.Parent
local throttle = vs.Throttle
local speed = script.Parent.Speed
while true do
if throttle == 1 then
speed.Value += 1
wait(0.1)
elseif throttle == -1 then
speed.Value -= 1
wait(0.1)
end
end
local run = game:GetService("RunService")
local speed = script.Parent.Speed
local Wheld = false
local interval = 1 -- Edit how fast it speeds up
local increment = 5 -- Edit how much speed gives
local timeSinceUpdate = tick()
run.Stepped:Connect(function()
if throttle == 1 then
if (tick() - timeSinceUpdate) < interval then return end
speed.Value += increment
print("Speed Increased to", speed.Value)
timeSinceUpdate = tick()
end
end)
Edit: modify some parts if you want,
just an advice, using RunService is faster and better than while do loop. However RunService only runs on the client