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

How to cram down this script so I dont have to write a lot of lines??

Hi. So like I am creating a Gun Spawner Gui. Since there is so much Guis, I dont think I should be writing them out too much. So I did this.

local Player = game:GetService("Players").LocalPlayer
local PGui = Player:WaitForChild("PlayerGui")

local tab = {
	[1] = PGui.ScreenGui["Ksg"],
	[2] = PGui.ScreenGui["HecateII"],
	[3] = PGui.ScreenGui["Intervention"],
	[4] = PGui.ScreenGui["AK104"],
	[5] = PGui.ScreenGui["HK416"],
	...
}

for i, v in pairs(tab) do
	v.MouseButton1Down:Connect(function()
		print(v.Name)
	end)
end

They work perfectly fine. But see, I want to create a spawning right. I dont want to make a big if statement checking the v's name. What i mean is like this.

if v.Name == "Ksg" then 
	Ksg:Clone()
elseif v.Name == "HecateII" then 
	HecateII:Clone()
...
end

I dont want to do it this way because I have a lot of guns and I think there is a better way to solve this but Idk how to do it.

Thank you if you can help me. :D

1 comment

0

Doing like you did with tab, you could also do the same for the weapons. :D Table['Gun_Name'] = Gun

2 answers

You can try to do something like this, It's up to you.

local Player = game:GetService("Players").LocalPlayer
local PGui = Player:WaitForChild("PlayerGui")
local SG = Player.PlayerGui:WaitForChild("ScreenGui")

local tab = {"Ksg","HecateII","Intervention","AK104","HK416"}

for i, q in pairs(tab) do
	local v = SG:FindFirstChild(q)
   	 v.MouseButton1Down:Connect(function()
      	  print(v.Name)
    	end)
end

Try this script!

Please accept this answer!

local Plr = game:GetService("Players").LocalPlayer
local PGui = Prr:WaitForChild("PlayerGui")
local SG = Plr.PlayerGui:WaitForChild("ScreenGui")

local tab = {"Ksg","HecateII","Intervention","AK104","HK416"}

for i, q in pairs(tab) do
    local v = SG:FindFirstChild(q)
     v.MouseButton1Down:Connect(function()
          print(v.Name)
        end)
end

3 comments

0

You literally copied and pasted the answer from @seith14

0

wow that is pathetic

0

amazing tactic

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