Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make this UserInput shorter?

Asked by 10 years ago
01local UserInput = game:GetService('UserInputService')
02 
03UserInput.InputBegan:connect(function(input)
04    if input.KeyCode == Enum.KeyCode.One then
05        script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.5
06        script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.7
07        script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7   
08        script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
09        script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7
10    elseif input.KeyCode == Enum.KeyCode.Two then
11        script.Parent.Inventory.MainFrame.InvSpace.Inv2.BackgroundTransparency = 0.5
12        script.Parent.Inventory.MainFrame.InvSpace.Inv1.BackgroundTransparency = 0.7
13        script.Parent.Inventory.MainFrame.InvSpace.Inv3.BackgroundTransparency = 0.7   
14        script.Parent.Inventory.MainFrame.InvSpace.Inv4.BackgroundTransparency = 0.7
15        script.Parent.Inventory.MainFrame.InvSpace.Inv5.BackgroundTransparency = 0.7
View all 35 lines...

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

2 answers

Log in to vote
1
Answered by
Nifer2 174
10 years ago
01local UserInput = game:GetService('UserInputService')
02local Inv1 = script.Parent.Inventory.MainFrame.InvSpace.Inv1
03local Inv2 = script.Parent.Inventory.MainFrame.InvSpace.Inv2
04local Inv3 = script.Parent.Inventory.MainFrame.InvSpace.Inv3
05local Inv4 = script.Parent.Inventory.MainFrame.InvSpace.Inv4
06local Inv5 = script.Parent.Inventory.MainFrame.InvSpace.Inv5
07 
08UserInput.InputBegan:connect(function(input)
09    if input.KeyCode == Enum.KeyCode.One then
10        Inv1.BackgroundTransparency = 0.5
11        Inv2.BackgroundTransparency = 0.7
12        Inv3.BackgroundTransparency = 0.7   
13        Inv4.BackgroundTransparency = 0.7
14        Inv5.BackgroundTransparency = 0.7
15    elseif input.KeyCode == Enum.KeyCode.Two then
View all 40 lines...

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. :/

1
yeah I would of liked less lines, but it's ok. I shall use the variables, don't tend to use variables much. NinjoOnline 1146 — 10y
0
Okay! Nifer2 174 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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

01local UserInput = game:GetService('UserInputService')
02local Inventory = script.Parent.Inventory.MainFrame.InvSpace
03 
04function EditInv(Num)
05    for i = 1,5 do --For loop that runs from 1 to 5
06        local Inv = Inventory["Inv"..i] --Finds the "Inv" with the number "i" after it
07        if i == Num then --If the located Inv is the Inv that you want to change...
08            Inv.BackgroundTransparency = 0.5
09        else --If it's not...
10            Inv.BackgroundTransparency = 0.7
11        end
12    end
13end
14 
15UserInput.InputBegan:connect(function(input)
View all 27 lines...

Answer this question