I'm trying to make a script that does the following things -add 4 gears to a car -gear 4 only works if you have a gamepass - all shifted with z/x or L1/R1
the script is part of my localcarscript that gets copied into the player's playergui folder when it is driven.
here is my code so far
1 | local player = game.Players.LocalPlayer |
2 | local stats = car:WaitForChild( "Configurations" ) |
3 | local CAS = game:GetService( "ContextActionService" ) |
4 | local staticspeed = stats.Speed.Value |
5 | local MPS = game:GetService( "MarketplaceService" ) |
6 | local cargear = script.Gear.Value |
01 | function geardown(name,state,action) |
02 | if state = = Enum.UserInputState.Begin then |
03 |
04 | elseif state = = Enum.UserInputState.End then |
05 | cargear = cargear - 1 |
06 | if cargear < 1 then |
07 | cargear = 1 |
08 | end |
09 |
10 | end |
11 | end |
12 |
13 | function gearup(name,state,action) |
14 | if state = = Enum.UserInputState.Begin then |
15 |
1 | CAS:BindActionToInputTypes( "GearDown" , geardown, true , Enum.KeyCode.Z, Enum.KeyCode.ButtonL 1 ) |
2 | CAS:SetPosition( "GearDown" , UDim 2. new(. 25 , 0 ,. 25 , 0 )) |
3 | CAS:SetTitle( "GearDown" , "Down" ) |
4 | CAS:BindActionToInputTypes( "GearUp" , gearup, true , Enum.KeyCode.X, Enum.KeyCode.ButtonR 1 ) |
5 | CAS:SetPosition( "GearUp" , UDim 2. new(. 10 , 0 ,. 25 , 0 )) |
6 | CAS:SetTitle( "GearUp" , "Up" ) |
when i run it in studio there is no output, and the car stays in gear 3 (which is the default value of the gear integer)
Line 6: local cargear = script.Gear.Value
This line returns an integer. Not the Value property of script.Gear, just an integer. When you change it like cargear = x
, you aren't updating the Value property of script.Gear, you're just updating that integer.
You need 'cargear' to be script.Gear
, and change it with cargear.Value = x
.
01 | local cargear = script.Gear |
02 |
03 | function gearup(name,state,action) |
04 | if state = = Enum.UserInputState.Begin then |
05 |
06 | elseif state = = Enum.UserInputState.End then |
07 | cargear.Value = cargear.Value + 1 |
08 | if cargear.Value > 4 then |
09 | cargear.Value = 4 |
10 | end |
11 |
12 | end |
13 | end |
as opposed to
01 | local cargear = script.Gear.Value |
02 |
03 | function gearup(name,state,action) |
04 | if state = = Enum.UserInputState.Begin then |
05 |
06 | elseif state = = Enum.UserInputState.End then |
07 | cargear = cargear + 1 |
08 | if cargear > 4 then |
09 | cargear = 4 |
10 | end |
11 |
12 | end |
13 | end |
Sorry if that explanation's overkill; I find this (quite common) issue quite finicky to put into words.
P.S. :connect()
is deprecated, use :Connect()
instead.