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

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

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

✓ Accepted answer

#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.

1 comment

0

would this work if the name is "DoubleMoneyIs"?

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