Yeah that fixed it but another problem appeared it is printing the name from RandomID function only to second print.
Try changing the function to this
function RandomID(Player, PlayerName)
print("1 " .. PlayerName)
local Rand = math.random(2,1000000)
print("2 " .. PlayerName)
local RequestedPlayer = game.Players:FindFirstChild(PlayerName) -- get the player object
if RequestedPlayer then -- check if the player is in the game
print("3 " .. PlayerName) -- move the print out here, if the player has no pets, the loop will auto-end
-- the loop won't run if there aren't any pets in their inventory
for i, v in pairs (RequestedPlayer.Pets:GetChildren()) do
print("4 " .. PlayerName)
if v.PetID.Value == Rand then
print("5 " .. PlayerName)
return RandomID(Player, PlayerName) -- put arguments in the function so it doesn't scream at you again
end
end
end
return Rand
end
Okay now I get to print 4th but then after the if statement print 5th doesnt working.
The 5th print is only going to print if the if
condition is true
This has to be passed as true
(meaning the PetId’s value has to be equivalent to the RandomNumber)
Yes I know but I have no clue why it isnt working I have everything correct PetChosen and Player(me).
This is probably due to the fact that if the statement does return true, it’ll cause an infinite loop since it would be calling the function over and over again.
When the function ends, the PetID’s value is set because the upper level function is waiting for the function to end before proceeding with the rest of the script
function RandomID()
return math.random(2,1000000) -- returns a random number between 2 and one million
end
local function getpetrandom(Player, PetChosen, PlayerName)
print(PetChosen.."1")
local Settings = RS.Pets.Models:FindFirstChild(PetChosen).Settings
PlayerName = game.Players:FindFirstChild(PlayerName) -- re-assign the PlayerName variable (because of later use)
if not PlayerName then return nil end -- the player doesn't exist
print(PetChosen.."2")
local Clone = RS.Pets.PetFolderTemplate:Clone()
Clone.PetID.Value = RandomID() -- assign a random ID
Clone.Multiplier1.Value = Settings.Multiplier1.Value
Clone.Multiplier2.Value = Settings.Multiplier2.Value
print(PetChosen.."3")
Clone.Type.Value = "Normal"
Clone.Parent = PlayerName.Pets -- before re-assigning the variable, "PlayerName" was just a string and not an object
print(PetChosen.."5")
Clone.Name = PetChosen
return PetChosen
end
Thank you soo soo much dude you solved it wish you great day and also thank you for time and effort that you put in it!