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

car gear script won't change gears. ?

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

local player = game.Players.LocalPlayer
local stats = car:WaitForChild("Configurations")
local CAS = game:GetService("ContextActionService")
local staticspeed = stats.Speed.Value
local MPS = game:GetService("MarketplaceService")
local cargear = script.Gear.Value

function geardown(name,state,action)
	if state == Enum.UserInputState.Begin then
		
	elseif state == Enum.UserInputState.End then
		cargear = cargear - 1
		if cargear < 1 then
			cargear = 1
		end
		
	end
end

function gearup(name,state,action)
	if state == Enum.UserInputState.Begin then
	
	elseif state == Enum.UserInputState.End then
		cargear = cargear + 1
		if cargear > 4 then
			cargear = 4
		end
		
	end
end

script.Gear.Changed:connect(function()
	if cargear == 1 then
		stats.Speed.Value = 20
	elseif cargear == 2 then
		if 55 > staticspeed then
			cargear = 3
		else
		stats.Speed.Value = 55
		end
	elseif cargear == 3 then
		stats.Speed.Value = staticspeed
	elseif cargear == 4 and MPS:UserOwnsGamePassAsync(player.UserId,6331302) then
		stats.Speed.Value = stats.Speed.Value + 35
	elseif cargear == 4 and not MPS:UserOwnsGamePassAsync(player.UserId,6331302) then
		cargear = 3
	end
end)
CAS:BindActionToInputTypes("GearDown", geardown, true, Enum.KeyCode.Z, Enum.KeyCode.ButtonL1) 
CAS:SetPosition("GearDown", UDim2.new(.25,0,.25,0))
CAS:SetTitle("GearDown", "Down")
CAS:BindActionToInputTypes("GearUp", gearup, true, Enum.KeyCode.X, Enum.KeyCode.ButtonR1) 
CAS:SetPosition("GearUp", UDim2.new(.10,0,.25,0))
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)

1 answer

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.

local cargear = script.Gear

function gearup(name,state,action)
    if state == Enum.UserInputState.Begin then

    elseif state == Enum.UserInputState.End then
        cargear.Value = cargear.Value + 1
        if cargear.Value > 4 then
            cargear.Value = 4
        end

    end
end

as opposed to

local cargear = script.Gear.Value

function gearup(name,state,action)
    if state == Enum.UserInputState.Begin then

    elseif state == Enum.UserInputState.End then
        cargear = cargear + 1
        if cargear > 4 then
            cargear = 4
        end

    end
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.

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