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 a more efficient script?

http://prntscr.com/a8zy10

I have a script, I am about to clone it into all the other buttons.

But i have an idea, I want to put the script in the frame, and do the code for all of the buttons in one script.

while true do
	wait()
	script.Parent.EffectLabel.Text = script.Parent.Text
	
	script.Parent.NameLabel.Text = script.Parent.nameValue.Value
	script.Parent.NameLabel.EffectLabel.Text = script.Parent.nameValue.Value
	
	script.Parent.CostLabel.Text = "["..script.Parent.costValue.Value.." points]"
		if script.Parent.costValue.Value == 0 then
			script.Parent.CostLabel.Text = "[free]"
		end
	script.Parent.CostLabel.EffectLabel.Text = script.Parent.CostLabel.Text
	
	if game.Players.LocalPlayer.leaderstats.Points.Value >= script.Parent.costValue.Value then
		script.Parent.CostLabel.TextColor3 = Color3.new(1,1,127/255) --has enough to purchase
	else 
		script.Parent.CostLabel.TextColor3 = Color3.new(1,100/255,100/255) --does not have enough to purchase
	end
	
end

Is there any way I can do this script, in all the buttons in the frame? In one script

1 answer

✓ Accepted answer
while true do
    wait()
for i,v in pairs(script.Parent:GetChildren()) do
   if v.ClassName == "Text Button" then
   v.EffectLabel.Text = script.Parent.Text

   v.NameLabel.Text = v.nameValue.Value
   v.NameLabel.EffectLabel.Text = v.nameValue.Value

    v.CostLabel.Text = "["..v.costValue.Value.." points]"
        if v.costValue.Value == 0 then
            v.CostLabel.Text = "[free]"
        end
    v.CostLabel.EffectLabel.Text = v.CostLabel.Text

    if game.Players.LocalPlayer.leaderstats.Points.Value >= v.costValue.Value then
        v.CostLabel.TextColor3 = Color3.new(1,1,127/255) --has enough to purchase
    else 
        v.CostLabel.TextColor3 = Color3.new(1,100/255,100/255) --does not have enough to purchase
end
    end
	end
end

Basically what

for i,v in pairs(script.Parent:GetChildren()) do
end

does is it classifies everything inside of script.Parent as V so you can make changes to everything at once.

if v.ClassName == "Text Button" then

Makes it so that it only changes text buttons (I'm assuming that's what you're using as buttons, if not change that classname to whatever it is, otherwise it won't work) without that, if you have anything in the Frame besides what you want the script to edit (even the script itself) then the script will break.

If it doesn't work, comment and tell me if there are any errors in the output. I'll try to fix it.

2 comments

0

Thank you, it worked! Line 5 had an error, which I fiexed, not script.Parent , but v.Parent

0

I guess I missed that. Well, either way, glad to hear that it works.

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