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

Why is my pet hatching system not working? It's not printing what I ask it to.

Nothing happens when I press the Gui Button, it is suppose to print the pet that it has chosen.

Module Script in ServerScriptService:

local petModule = {}

petModule.pets = {
	
	["Godly"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Aqua Dragon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bacterial Virus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Demon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Demonic Destroyer");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Dominus Ultimus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Electric spider");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Forgotten kraken");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Gigantic Lord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Kraken");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Lava Lord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Mythical Demon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Plasma Overlord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Shadow Dominus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Ultimus");
	};
	
	["Legendary"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Alien");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angel");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angeler fish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Angelfly");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Candy Mage");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Creature of light");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Demonic Dominus");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Duo Fly");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Infection");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Lava Beast");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("OctoHex");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Octopus Lord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Robot Overlord");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Skeleton Fly");
	};
	
	["Rare"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Baby lighting");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bacterium");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Candy Stack");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Crab");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Creature of the deap");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cyborg");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Demon bee");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Demonic Spider");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Fedora");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Hallows spider");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Ice golem");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Jellyfish");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Light Bat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Nice cloud");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Psychic");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Psychic Scorpion");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Skeleton deer");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Skeleton pup");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Spikey Balloon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Starry");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("TV");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lil' Dragon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lil' creature");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lil' demon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lil' fly");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("lil' sea monster");
	};
	
	["Uncommon"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Builder");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bull");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Burger");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cowboy");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Deviloon");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Donut");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Golden Cat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Gumdrop");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Headphones");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Hellish Golem");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Mad Scientist");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Monster");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Ninja");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Pepper");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Reaper");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Robot");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Shark");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Snowman");
	};
	
	["Common"] = {
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bear");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bee");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Bunny");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cat");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Cow");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Dog");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Duck");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Fox");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Koala");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Monkey");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Mouse");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Panda");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Pig");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Reindeer");
		game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild("Turkey");
	};
	
	
	
}

petModule.rarities = {
	
	["Godly"] = 5;
	
	["Legendary"] = 10;
	
	["Rare"] = 15;
	
	["Uncommon"] = 20;
	
	["Common"] = 50;
	
}

petModule.chooseRandomPet = function()
	
	local randomNumber = math.random(1,100)
	print(randomNumber)
	local counter = 0
	
	for rarity, weight in pairs(petModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			
			local rarityTable = petModule.pets[rarity]
			local chosenPet = rarityTable[math.random(1,#rarityTable)]
			
			return chosenPet
			
		end
		
	end

end

return petModule

Script parented to a Gui TextButton

local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))

script.Parent.Activated:Connect(function(player)
	
	if player.leaderstats.SizzleBucks.Value >= 500 then
		
		player.leaderstats.SizzleBucks.Value = player.leaderstats.SizzleBucks.Value - 500
		
		local pet = petModule.chooseRandomPet()
		
		print(pet.Name.." selected")
		
	else
		print("Pet failure")
	
	end
	
end)

2 answers

✓ Accepted answer

your code is fine but the problem is the script that is inside of a textbutton should be a LocalScript not a normal script but if its already a localscript then the problem is strange

8 comments

0

I'm watching a YouTube tutorial and he did it in a server script though.

0

anything inside the starter gui should be a local script

0

just do what i say trust me

0

It still dooesn't work in LocalScript.

0

what did you convert to a local script?

0

or just add me in discord mrwan#2646

0

I copy and pasted everything in the server script to a local script and it still didn't work

0

add me in discord mrwan#2646

I think it may be because of this line:

local chosenPet = rarityTable[math.random(1,#rarityTable)]

You're looking back through the rarityTable, shouldn't you be checking the actual pet table like this?:

local rarityTable = petModule.pets[rarity]
local chosenPet = rarityTable[math.random(1,#rarityTable)]

4 comments

0

Still doesn't work, but thanks for trying.

0

Oh, I see why. My bad. Let me edit this.

0

Oh wait, no. Your code is right. Do you not get any errors? What's the output to the print?

0

When I try to print something it doesn't print at all, it's strange.

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