Ad
Log in to vote
0

I made a little script but is there a shortcut for repeating the lines over again, help?

Asked by 12 days ago

So here is the script I made and instead of doing line 1, 2 and 3 again can I look for the word Is in the value name and make that value false? If you can then can you help me on understanding to do it? I'm looking for a shortcut because soon I will add way more than 4 perks and would help a lot.

local TheName = -- Player's username

while true do
    wait()
    if game.Players:FindFirstChild(TheName).PerkValues.DoubleJumpGlideIs.Value == true then
        game.Players:FindFirstChild(TheName).PerkValues.MoreHealthIs.Value = false -- line 1
        game.Players:FindFirstChild(TheName).PerkValues.DoubledMoneyIs.Value = false -- line 2
        game.Players:FindFirstChild(TheName).PerkValues.DoubledDamageIs.Value = false -- line 3
                print("All other perks Unequipped")
    else
        print("Hi")
        wait(5)
    end
end

1 answer

Log in to vote
1
Answered by
Benbebop 949 Moderation Voter
12 days ago
Edited 12 days ago

Certainly possible

But in the end it will likely take more space than your code already does. But it is good for the sake of future proofing your code (say you add another "Is" value, this would already cover that).

How It's Done

First you will have to get acquainted with string. It allows for many different ways of manipulating strings but today we will be covering match.

Basically match allows us to search a string for a pattern and if it finds that pattern, return it.

Now lets get some code down:

local perks = game.Players:FindFirstChild(TheName).PerkValues:GetChildren()

for i=1,#perks do

end

We store all the children of your PerkValues instance in a variable. Since GetChildren returns a table we can read how long it is and iterate for each table index.

Now lets see if our instance has a name with "Is" in it.

local perks = game.Players:FindFirstChild(TheName).PerkValues:GetChildren()

for i=1,#perks do
    if string.match(perks[i].Name, "Is") ~= nil then

    end
end

Basically what we did is create an if statement and then matched the current perk's name with the string "Is". Since this returns ether the matched string (if it was found) or nil (if nothing was found), we can't treat it as a simple bool statement.

With ~= we see if the result is not equal to nil. So if string.match returns nil, ~= will return false; if string.match returns anything but nil, ~= returns true.

Thats really it, place your value modifying code (value = false) inside the if statement and your off.

0
would this work if the name is "DoubleMoneyIs"? GameStealerKid 57 — 12d
Ad

Answer this question