Setting variable with W

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)

it has to modify the variable named Speed

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)

this could be exploitable

They could press W repeteadly, while the while wait(1) do loop will wait for a second before checking

  1. Player Press W
  2. While wait runs
  3. Player Repeats step 1 continuously for a second
  4. Waits for 1 second
  5. 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

hope this helps

it doesn’t work, when i sit down and pres w the value doesn’t go up

is there an error? because when I tested it, it works

i’ll check again, i dont know how, but i use a local script

and it is just a normal seat. so i dont know if that is the problem

try this, I just added prints so we can debug it

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

it doesn’t even says “W Key held”

try removing

if gp then return end

on uis.InputBegan and uis.InputEnded

i still get no sign it works, but i also see no reference to the seat, is that right?


this is the situation

can i use a vehicle seat and use the throttle on that?

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

this but then working

is that working like 100% when you move? because if yes then we could just use that instead of the uis.InputBegan

no not yey, but i can fix that, i used throttle first in an other script, i just dont know if i do that right

sorry im not really good at using interaction objects so i might not be able to help you with what you want

ok i think i can fix it. i used it before. but thanks to you i know how to find the bugs

maybe try this? i just changed uis to throttle

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

1 Like

thank i’ll try that very very soon