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

How to make this UserInput shorter?

local UserInput = game:GetService('UserInputService')

UserInput.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.One then
		script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.5
		script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7	
		script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7
    elseif input.KeyCode == Enum.KeyCode.Two then
		script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.5
		script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7	
		script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7
	elseif input.KeyCode == Enum.KeyCode.Three then
		script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.5
		script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.7	
		script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7
	elseif input.KeyCode == Enum.KeyCode.Four then
		script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.5
		script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.7	
		script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7	
	elseif input.KeyCode == Enum.KeyCode.Five then
		script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.5
		script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.7	
		script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7
		script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
    end
end)

How could I possibly make this shorten, but not change what the outcome is?

2 answers

✓ Accepted answer
local UserInput = game:GetService('UserInputService')
local Inv1 = script.Parent.Inventory.MainFrame.InvSpace.Inv1
local Inv2 = script.Parent.Inventory.MainFrame.InvSpace.Inv2
local Inv3 = script.Parent.Inventory.MainFrame.InvSpace.Inv3
local Inv4 = script.Parent.Inventory.MainFrame.InvSpace.Inv4
local Inv5 = script.Parent.Inventory.MainFrame.InvSpace.Inv5

UserInput.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.One then
        Inv1.BackgroundTransparency = 0.5
        Inv2.BackgroundTransparency = 0.7
        Inv3.BackgroundTransparency = 0.7    
        Inv4.BackgroundTransparency = 0.7
        Inv5.BackgroundTransparency = 0.7
    elseif input.KeyCode == Enum.KeyCode.Two then
        Inv2.BackgroundTransparency = 0.5
        Inv1.BackgroundTransparency = 0.7
        Inv3.BackgroundTransparency = 0.7    
        Inv4.BackgroundTransparency = 0.7
        Inv5.BackgroundTransparency = 0.7
    elseif input.KeyCode == Enum.KeyCode.Three then
        Inv3.BackgroundTransparency = 0.5
        Inv1.BackgroundTransparency = 0.7
        Inv2.BackgroundTransparency = 0.7    
        Inv4.BackgroundTransparency = 0.7
        Inv5.BackgroundTransparency = 0.7
    elseif input.KeyCode == Enum.KeyCode.Four then
       Inv4.BackgroundTransparency = 0.5
        Inv1.BackgroundTransparency = 0.7
        Inv2.BackgroundTransparency = 0.7    
        Inv3.BackgroundTransparency = 0.7
       Inv5.BackgroundTransparency = 0.7    
    elseif input.KeyCode == Enum.KeyCode.Five then
        Inv5.BackgroundTransparency = 0.5
        Inv1.BackgroundTransparency = 0.7
        Inv2.BackgroundTransparency = 0.7    
        Inv3.BackgroundTransparency = 0.7
        Inv4.BackgroundTransparency = 0.7
    end
end)

I just created variables. You should try using variables more often so you can type less. If less lines was what you wanted then I really can't help you with that. :/

2 comments

1

yeah I would of liked less lines, but it's ok. I shall use the variables, don't tend to use variables much.

0

Okay!

I pretty much cut the original script in half by getting rid of the repetition and replacing it with a function

local UserInput = game:GetService('UserInputService')
local Inventory = script.Parent.Inventory.MainFrame.InvSpace

function EditInv(Num)
	for i = 1,5 do --For loop that runs from 1 to 5
		local Inv = Inventory["Inv"..i] --Finds the "Inv" with the number "i" after it
		if i == Num then --If the located Inv is the Inv that you want to change...
			Inv.BackgroundTransparency = 0.5
		else --If it's not...
			Inv.BackgroundTransparency = 0.7
		end
	end
end

UserInput.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		EditInv(1)
	elseif input.KeyCode == Enum.KeyCode.Two then
		EditInv(2)
	elseif input.KeyCode == Enum.KeyCode.Three then
		EditInv(3)
	elseif input.KeyCode == Enum.KeyCode.Four then
		EditInv(4)
	elseif input.KeyCode == Enum.KeyCode.Five then
		EditInv(5)
	end
end)

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