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

how to make this script simple ?

im going to have 100+ upgrades so i need it more simple but im not sure how to use ** for i = 1,3**

maybe something like this

upgrade = upgrade:GetChildren()
for i = 1,#upgrade do

end
local gui = script.Parent
local core = gui.Core
local upgrade = gui.Upgrades
local background = gui.Background

core.upgrade.Changed:connect(function()
if core.upgrade.Value == 1 then
	core.cost.Value = upgrade.upgrade1.Cost.Value
	core.clickdmg.Value = core.clickdmg.Value + upgrade.upgrade2.DMG.Value
	elseif core.upgrade.Value == 2 then
	core.cost.Value = upgrade.upgrade2.Cost.Value
	core.clickdmg.Value = core.clickdmg.Value + upgrade.upgrade2.DMG.Value
	elseif core.upgrade.Value == 3 then
	core.cost.Value = upgrade.upgrade3.Cost.Value
	core.clickdmg.Value = core.clickdmg.Value + upgrade.upgrade3.DMG.Value
	elseif core.upgrade.Value == 4 then
	core.cost.Value = upgrade.upgrade4.Cost.Value
	core.clickdmg.Value = core.clickdmg.Value + upgrade.upgrade4.DMG.Value
	elseif core.upgrade.Value == 5 then
	core.cost.Value = upgrade.upgrade5.Cost.Value
	core.clickdmg.Value = core.clickdmg.Value + upgrade.upgrade5.DMG.Value
end
end)

2 answers

I moved all of the if then stuff into a function. So because you were having the upgrade number = to the if then I just moved the number into the name of the child. [] is an easy way to find the child with the use of a string.

local gui = script.Parent
local core = gui.Core
local upgrade = gui.Upgrades
local background = gui.Background

function Exic(cor)
core.cost.Value = upgrade["upgrade"..tostring(cor)].Cost.Value --if core.upgrade.Value = 1 then upgrade1 and exetra.
core.clickdmg.Value = core.clickdmg.Value + upgrade["upgrade"..tostring(cor)].DMG.Value
end

core.upgrade.Changed:connect(function()
if core.upgrade.Value ~= nil then --makes sure core.upgrade.Value is a number.
Exic(core.upgrade.Value)--fires the function and sends core.upgrade.Value
end
end)

your second question;

local deb = true -- so you can't click the button before the text changes back to upgrade.
background.Upgrades.Upgrade.MouseButton1Up:connect(function()
if deb = true then
deb = false
	if core.gold.Value >= core.cost.Value then
	    	core.gold.Value = core.gold.Value - core.cost.Value
	    	core.upgrade.Value = core.upgrade.Value + 1
		background.Upgrades.Upgrade.Text = "Bought"
	elseif core.gold.Value < core.cost.Value then
	        background.Upgrades.Upgrade.Text = "Need more Gold"
	end
wait(1)
background.Upgrades.Upgrade.Text = "Upgrade"--I just moved this here. reduces 2 lines
    if core.upgrade.Value > core.maxupgrade.Value then
        core.upgrade.Value = core.maxupgrade.Value
        core.cost.Value = 645737568
    end
deb = true
end
end)

4 comments

0

thank you !! where could i learn this ?

0

hmm learn this...... I kind of picked up on this stuff by looking at other people's scripts, asking questions, and most importantly putting new learned info into the scripts I make or have made.

0

ah, thats kind of what i do haha

0

And here I thought I was the only one that taught myself with free models...

sorry i forgot to post this but could you do anything with this ? (same script)

background.Upgrades.Upgrade.MouseButton1Up:connect(function()
	if core.gold.Value >= core.cost.Value then
	core.gold.Value = core.gold.Value - core.cost.Value
	core.upgrade.Value = core.upgrade.Value + 1
	background.Upgrades.Upgrade.Text = "Bought"
		wait(1)
		background.Upgrades.Upgrade.Text = "Upgrade"
	elseif core.gold.Value < core.cost.Value then
		background.Upgrades.Upgrade.Text = "Need more Gold"
		wait(1)
		background.Upgrades.Upgrade.Text = "Upgrade"
	end
	if core.upgrade.Value > core.maxupgrade.Value then
		core.upgrade.Value = core.maxupgrade.Value
		core.cost.Value = 645737568
	end
end)

1 comment

0

there are not many ways to make this better, but I simplified this in my answer above.

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