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

How to make a more efficient script?

Asked by 8 years ago

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

Log in to vote
0
Answered by
Scootakip 299 Moderation Voter
8 years ago
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.

0
Thank you, it worked! Line 5 had an error, which I fiexed, not script.Parent , but v.Parent ConnorVIII 448 — 8y
0
I guess I missed that. Well, either way, glad to hear that it works. Scootakip 299 — 8y
Ad

Answer this question