How do i simplify these gamepad controls?

This is a localscript that goes inside a skateboard thing, and i am incredibly unsatisfied at how many lines of code it uses for the damn thing, it basically just adds button + joystick combos to do tricks on a skateboard. I would love to be able to make this easier to work with for like combos and stuff etc

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Position.Y >= .9 then
				up = true
				down = false
				upleft = false
				upright = false
				downleft = false
				downright = false
			elseif input.Position.Y <= .8 then
				up = false
			end
			if input.Position.Y <= -.9 then
				down = true
				up = false
				upleft = false
				upright = false
				downleft = false
				downright = false
			elseif input.Position.Y >= -.8 then
				down = false
			end
			if input.Position.X <= -.13 and input.Position.X >= -.65 and input.Position.Y >= .1 and input.Position.Y <= .65 then
				upleft = true
				down = false
				up = false
				upright = false
				downleft = false
				downright = false
			elseif input.Position.X >= -.1 and input.Position.X <= -.65 and input.Position.Y <= .1 and input.Position.Y >= .65 then
				upleft = false
			end
			if input.Position.X <= .1 and input.Position.X >= .65 and input.Position.Y >= .1 and input.Position.Y <= .65 then
				upright = true
				down = false
				up = false
				upleft = false
				downleft = false
				downright = false
			elseif input.Position.X >= .1 and input.Position.X <= .65 and input.Position.Y <= .1 and input.Position.Y >= .65 then
				upright = false
			end
			if input.Position.X <= -.1 and input.Position.X >= -.65 and input.Position.Y >= .1 and input.Position.Y <= -.65 then
				downleft = true
				down = false
				up = false
				upleft = false
				upright = false
				downright = false
			elseif input.Position.X >= -.1 and input.Position.X <= -.65 and input.Position.Y <= .1 and input.Position.Y >= -.65 then
				downleft = false
			end
			if input.Position.X <= .1 and input.Position.X >= -.65 and input.Position.Y >= .1 and input.Position.Y <= -.65 then
				downright = true
				down = false
				up = false
				upleft = false
				upright = false
				downleft = false
			elseif input.Position.X >= .1 and input.Position.X <= -.65 and input.Position.Y <= .1 and input.Position.Y >= -.65 then
				downright = false
			end
		end
	end
end)
UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Position.X <= -.9 then
				left = true
				--print("left = true")
			elseif input.Position.X >= -.8 then
				left = false
				--print("left = false")

			end
		end
	end
end)
UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Position.X >= .9 then
				right = true
				--print("right = true")
			elseif input.Position.X <= .8 then
				right = false
				--print("right = false")
			end
		end
	end
end)
local db789 = false -- this is just debug to see how the inputs

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if not db789 then
				print(input.Position.X, input.Position.Y)
				db789 = true
				wait(.5)
				db789 = false
			end
			
		end
	end
end)

local db = false
local onground = true
while wait(.1) do
	if B == true then
		push()
	end
	if B == false then
		coast()
	end
	if LB == true then
		spinLeft()
	end
	if LB == false then
		stopSpin()
	end
	if RB == true then
		spinRight()
	end
	if RB == false then
		stopSpin()
	end
	if Ddown == true then
		leanBackwards()
	end
	if Dup == true then
		leanFowards()
	end
	if Y == true then 
		slowdown()
	end
	if X == true and right == true then
		if not db then
			heelflip()
			db = true
			wait(.3)
			db = false
		end
	end	
	if X == true and left == true then
		if not db then	
			kickflip()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and up == true then
		if not db then 
			impossible()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and down == true then
		if not db then
			shuvit()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and upleft == true then
		if not db then
			laserflip()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and upright == true then
		if not db then
			hardflip()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and downleft == true then
		if not db then
			kickunder()
			db = true
			wait(.3)
			db = false
		end
	end
	if X == true and downright == true then
		if not db then
			treflip()
			db = true
			wait(.3)
			db = false
		end
	end
end

1 Like

I mean to get started, guess you could move this into a function

up = true
down = false
upleft = false
upright = false
downleft = false
downright = false

Since you use that except making one true.

local function makeFalse()
    up = false
    down = false
    upleft = false
    upright = false
    downleft = false
    downright = false
end

...
if input.Position.Y >= .9 then
    makeFalse()
    up = true
end

As for the bindable event, guess you could potentially use some sort of script bindable event in order to detect when your set variables are changed rather than using a while loop to check.

Yeah that would clean up quite a bit, thanks.