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

Gotta be a better way to do this?

Asked by 8 years ago

I think I have created the most inefficient er, well at least most grinding way to make weapon attachments ever. I have a different gun each with a different attachment, currently only 3 guns. But all I want is a faster way to do this script like more organized or something. What I have are 3 buttons, two attachments and a gun. I also have two text labels that show what you pressed. The buttons change the text in the text labels, this script looks for the names to determine what gun to give. Well here is the code, I was wondering if there was a faster way to check or more efficient or somethng lol I don't even know:

local gun = script.Parent.Parent.Selected
local ment = script.Parent.Parent.Attach
local ACR = game.ReplicatedStorage.Weapons.ACR
local ACRA = game.ReplicatedStorage.Weapons.ACRAcog
local ACRR = game.ReplicatedStorage.Weapons.ACRDot
local Aclone = ACR:Clone()
local AAclone = ACRA:Clone()
local ADclone = ACRR:Clone()
local player = game.Players.LocalPlayer
local gui = script.Parent.Parent


function onClicked()
if gun.Text == "ACR" then
    if ment.Text == "None" then
        Aclone.Parent = player.Backpack
        gui:Destroy()
elseif ment.Text == "Acog" then
    AAclone.Parent = player.Backpack
    gui:Destroy()
elseif ment.Text == "Red Dot" then
    ADclone.Parent = player.Backpack
    gui:Destroy()
end
end
end

script.Parent.MouseButton1Down:connect(onClicked)

As you see, it sucks lol a different variable for each gun and the clone. Just wondering if there was a faster and better way to do all of this. Any help is greatly appreciated :)

1 answer

Log in to vote
1
Answered by 8 years ago

Fortunately you've already got a good naming system. Now you just need to use it like this:

--Keep lines 1, 2, 9, and 10
local weapons = game.ReplicatedStorage.Weapons
function onClicked()
    local gunModel = weapons[gun.Text .. (ment.Text == "None" and "" or ment.Text)]
    gunModel:Clone().Parent = player.Backpack
    gui:Destroy()
end

Essentially, instead of writing out every name combination by hand, I've combined the variables together to get the proper name. (.. is the string concatenation operator -- ie it adds strings together)

ex, in the case that gun.Text is "ACR" and ment.Text is "Acog", the line above will evaluate like so:

weapons[gun.Text .. (ment.Text == "None" and "" or ment.Text)]
weapons["ACR" .. ("Acog" == "None" and "" or "Acog")]
weapons["ACR" .. ("Acog")]
weapons["ACRAcog"]

Note the line with "and or" in it. That's sort of like an in-line if. You use it like this:

condition and value1 or value2

If condition evaluates to true (in other words, if condition is anything except nil or false), this expression will return value1. Otherwise, it returns value2. However, you must not let value1 be false or nil, or else you'll get value2 regardless of condition.

The code I provided above will error if you don't have the proper model. You might want to use :FindFirstChild instead and give the user some error if the gun "wasn't found".

Ad

Answer this question