Help me with the Script please!

  1. What do you want to achieve? I want to take 800 BugCoins from the leaderboard when i click the button.

  2. What is the issue? robloxapp-20200729-2010290.wmv (2.3 MB)

  3. What solutions have you tried so far? I tried to move the script.

Here is the Script

local cost = 800

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

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	
	if player.leaderstats.BugCoins.Value >= cost then
		
		player.leaderstats.BugCoins.Value = player.leaderstats.BugCoins.Value - cost
		
		local pet = PetModule.chooseRandomPet()
		
		print(pet.Name.." selected")
	end
end)

Here is the ModueScript/PetModule

local petModule = {}

petModule.pets = {
	
	["Mythic"] = {
		
	};
	
	["Legendary"] = {
		
	};
	
	["Epic"] = {
		
	};
	
	["UnCommon"] = {
		game.ReplicatedStorage.Pets.BlackCaterpillar;
		game.ReplicatedStorage.Pets.WhiteCaterpillar;
	};
	
	["Common"] = {
		game.ReplicatedStorage.Pets.GreenCaterpillar;
		game.ReplicatedStorage.Pets.DarkGreenCaterpillar;
		game.ReplicatedStorage.Pets.LightGreenCaterpillar;
	};

}

petModule.rarities = {
	
	["Mythic"] = 1;
	
	["Legendary"] = 3;
	
	["Epic"] = 17;
	
	["UnCommon"] = 24;
	
	["Common"] = 55;
	
}

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

return module

I found a typo in local rarityTable = petModule.pet[rarity]. You didn’t spell the name of the dictionary.

Also I recommend looking at your output it is an easy fix.

Change the line to local rarityTable = petModule.pets[rarity]

1 Like

The output is empty. I looked at that.

1 Like