Make A Script Do Something A Certain Amount of Times

Hello!

I wanted to make a Equip Best Pet system, but I’m not sure how to make it so where the script only equips the amount of pet equips the player has (does something a certain amount of times) and stops right after.

Here’s my current script:

function ActionPetRequest(Player, Action, Parameters) --equip best pet
	if Action == "Equip Best" then

	for i,v in pairs(Player.Pets:GetChildren()) do
	local TotalEquipped = 0
	local Folder = GetFolderFromPetID(Player, Parameters.PetID)
		local Multiplier1 = v.Multiplier1
		print(Multiplier1.Value)
		if TotalEquipped < Player.Data.MaxEquip.Value then -- if total equipped is less then max equipped then
				local Equippable = Player.Data.MaxEquip.Value
				v.Equipped.Value = true -- equip pet
				TotalEquipped += Player.Data.MaxEquip.Value
				loadEquipped(Player) -- load pets 
				Player.PlayerGui.Main.PetInventory.MainFrame.EquippedDisplay.TextLabel.Text = TotalEquipped .. "/" .. Player.Data.MaxEquip.Value
break
				end
				
			end
		end
	
		end
		end

I’ve tried a ton of ways, and I looked all over the dev hub and could not find any solutions.

Thanks!
video:
robloxapp-20230818-1818026.wmv (1.7 MB)

7 Likes

use a for loop (wordadsssssss)

1 Like

Use the “TotalEquipped” variable and also make a “MaxEquipped” variable which defines how many pets are to be equipped.

local TotalEquipped = 0
local MaxEquipped = 4 -- put whatever you need

repeat
    wait()
    TotalEquipped = TotalEquipped + 1
    -- your best pet equip process here
until TotalEquipped == MaxEquipped

This should work.

4 Likes

Thanks for responding!

I’ve tried implementing that to the script, and it doesn’t seem to work really, it keeps equipping the pets.

This is the script with the things you said to add:

local TotalEquipped = 0
	local Folder = GetFolderFromPetID(Player, Parameters.PetID)

			if TotalEquipped < Player.Data.MaxEquip.Value then -- if total equipped is less then max equipped then
				repeat
					wait()
				local MaxEquipped = Player.Data.MaxEquip
				v.Equipped.Value = true -- equip pet
				TotalEquipped = TotalEquipped + 1
				loadEquipped(Player) -- load pets 
				Player.PlayerGui.Main.PetInventory.MainFrame.EquippedDisplay.TextLabel.Text = TotalEquipped .. "/" .. Player.Data.MaxEquip.Value
				until TotalEquipped == MaxEquipped.Value

				
			end
		end
	end
		end

Do you have any idea of why this happens?

Thanks!
video:
robloxapp-20230818-1919249.wmv (1.1 MB)

1 Like

What is MaxEquip.Value in the video? Is it 9?
So, you had 10 pets equipped at the end and I know why this is caused. I provided you with faulty code, sorry.
Put TotalEquipped on standard to 1 (so when you define the variable) or say:

until (TotalEquipped - 1) == MaxEquipped.Value

I don’t know if this fixes the problem with the continious looping but as the process counts the 0 with it and executes the equip best pet process with it, it was executed 10 times after code and not 9 times.

If the issue with the looping of pet continues, reply to this and I’ll see what I can do.

2 Likes

By the way, I looked at your code and to make your system more stable I would recommend you to make a new Value in “Player.Data” called “TotalEquipped”. In your code you update a TextLabel to display the correct number of pets equipped.
You will probably later need to know how many pets are equipped by the player right now. It makes things way easier and instead of having to manually update the TextLabel when you change the pets that are equipped by the player, I would recommend you to write a script that listens to the “Player.Data.TotalEquipped” and focusses on the “Value” and as soon as the value gets changed the script updates the TextLabel automatically.
This is how I perceive it but maybe you have other plans in mind.

2 Likes

Thanks for responding!

I’ve implemented your suggestions of changing it to until (TotalEquipped.Value - 1) == MaxEquipped.Value and making “TotalEquipped” a value. It still doesn’t seem to be working for some reason. It seems to only equip one pet, and says, “10/9 pets equipped”. If you have any solution to why this won’t work, please let me know!

Script:

function ActionPetRequest(Player, Action, Parameters) --equip best pet
	if Action == "Equip Best" then

	for i,v in pairs(Player.Pets:GetChildren()) do
	local TotalEquipped = Player.Data.CurrentEquipped
	local Folder = GetFolderFromPetID(Player, Parameters.PetID)
		local Multiplier1 = v.Multiplier1
			print(Multiplier1.Value)

			if TotalEquipped.Value < Player.Data.MaxEquip.Value then -- if total equipped is less then max equipped then
				repeat
					wait()
				local MaxEquipped = Player.Data.MaxEquip
				v.Equipped.Value = true -- equip pet
				TotalEquipped.Value = TotalEquipped.Value + 1
				loadEquipped(Player) -- load pets 
				Player.PlayerGui.Main.PetInventory.MainFrame.EquippedDisplay.TextLabel.Text = TotalEquipped.Value .. "/" .. Player.Data.MaxEquip.Value
				until (TotalEquipped.Value - 1) == MaxEquipped.Value

				
			end
		end
	end
		end

video:
robloxapp-20230818-2136374.wmv (583.5 KB)

Thanks!

1 Like

Okay so you changed alot in your code. You implemented the TotalEquipped as an actual value stored in Player.Data.

This changes a few things and we now can’t just say

until (TotalEquipped.Value - 1) == MaxEquipped.Value

Your code is also a bit messy let me get through that, and run it again. I am pretty sure that you will have an issue again but also tell me the value of the elements stored externally (especially those saved in Player.Data).

This is your code in “fixed”:

function ActionPetRequest(Player, Action, Parameters)
	if Action == "Equip Best" then

		for i,v in pairs(Player.Pets:GetChildren()) do
			local TotalEquipped = Player.Data.CurrentEquipped
			local Folder = GetFolderFromPetID(Player, Parameters.PetID)
			local Multiplier1 = v.Multiplier1
			print(Multiplier1.Value)

			if TotalEquipped.Value < Player.Data.MaxEquip.Value then
				repeat
					wait()
					TotalEquipped.Value = TotalEquipped.Value + 1
					if TotalEquipped.Value >=1 then
						local MaxEquipped = Player.Data.MaxEquip
						v.Equipped.Value = true
						loadEquipped(Player)
						Player.PlayerGui.Main.PetInventory.MainFrame.EquippedDisplay.TextLabel.Text = TotalEquipped.Value .. "/" .. Player.Data.MaxEquip.Value
					end
					
				until TotalEquipped.Value == MaxEquipped.Value -- we can use it like this now
				
			end
		end
	end
end

Run again. Now you finally shouldnt run into the 10/9 issue but probably only 1 pet will still be equipped. report to me

2 Likes

Hello!

My friend found a solution to this! He said that I should use a loop instead of using a repeat until.

script:

function ActionPetRequest(Player, Action, Parameters)
   if Action == "Equip Best" then
       
       local TotalEquipped = Player.Data.CurrentEquipped
   	
   	local MaxEquipped = Player.Data.MaxEquip
   	
       local pets = Player.Pets:GetChildren()
       
       for i = TotalEquipped.Value+1, MaxEquipped.Value do			
   		
           local v = pets[i]
           
           TotalEquipped.Value = TotalEquipped.Value + 1
   		if TotalEquipped.Value >=1 then
   			local Multiplier1 = v.Multiplier1
   			
   			table.sort(pets,function(a, b)
   				return a.Multiplier1.Value > b.Multiplier1.Value
   			end)	
               v.Equipped.Value = true
               loadEquipped(Player)
               Player.PlayerGui.Main.PetInventory.MainFrame.EquippedDisplay.TextLabel.Text = TotalEquipped.Value .. "/" .. Player.Data.MaxEquip.Value
   		end
   			
       end
   end
end

Thanks for all the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.