Script isn't working or printing

Do this as well, yeah. Then, it should be working.

1 Like

Your module script should look like this:

local BasicEgg = {}


BasicEgg.Blocks = {

	["Godly"] = {

	};

	["Legendary"] = {

	};

	["Epic"] = {

	};

	["Rare"] = {

	};

	["Uncommon"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("PurpleBlock")
	};

	["Common"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("")
	};
}

BasicEgg.Rarities = {

	["Godly"] = 0.25;

	["Legendary"] = 2.75;

	["Epic"] = 5;

	["Rare"] = 15;

	["Uncommon"] = 25;

	["Common"] = 52;

}

BasicEgg.ChooseRandomBlock = function()

	local random = math.random(1,100)

	local counter = 0

	for rarity, weight in pairs(BasicEgg.Rarities) do
		counter = counter + weight
		if random <= counter then

			local raritytable = BasicEgg.Block(rarity)
			local chosenblock = raritytable[math.random(1, #raritytable)]

			return chosenblock
		end
	end
end




return BasicEgg

now it says in output: ReplicatedStorage.Basic:57: attempt to index nil with 'Legendary'

omg man don’t tell me I gotta make a egg for each damn tier.

Very weird, I’m not having an error in the output, seems to be working fine.

Can I see where your script is located?

ok, I moved this module script to serverscirptservice cause thats were you told me to move it now I got:ServerScriptService.Basic:57: attempt to index nil with 'Common''

This is how everything should look:

image
(Part is inside Workspace)
Script contents:

print("im quirky for writing this way")
local cost = 500
local blockModule = require(game:GetService("ServerScriptService"):WaitForChild("Basic"))

local clickieClick = script.Parent:WaitForChild("ClickDetector")

clickieClick.MouseHoverEnter:Connect(function(player)
	print("I have lost all my sanity")
	if player.leaderstats["Bloxy Cash"].Value >= cost then
		player.leaderstats["Bloxy Cash"].Value = player.leaderstats["Bloxy Cash"].Value - cost
		print("AAAAAAAAAAAAAAAAAAAAAAAAAAA")
		local block = blockModule.ChooseRandomBlock()

		print(block.Name)
	end
end)

image

image
Basic Module Script Contents:

local BasicEgg = {}


BasicEgg.Blocks = {

	["Godly"] = {

	};

	["Legendary"] = {

	};

	["Epic"] = {

	};

	["Rare"] = {

	};

	["Uncommon"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("PurpleBlock")
	};

	["Common"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("")
	};
}

BasicEgg.Rarities = {

	["Godly"] = 0.25;

	["Legendary"] = 2.75;

	["Epic"] = 5;

	["Rare"] = 15;

	["Uncommon"] = 25;

	["Common"] = 52;

}

BasicEgg.ChooseRandomBlock = function()

	local random = math.random(1,100)

	local counter = 0

	for rarity, weight in pairs(BasicEgg.Rarities) do
		counter = counter + weight
		if random <= counter then

			local raritytable = BasicEgg.Block(rarity)
			local chosenblock = raritytable[math.random(1, #raritytable)]

			return chosenblock
		end
	end
end




return BasicEgg

Try copy/pasting my version of each script and put them in the correct location, let me know if this works.

yes, I already have a leader stats, ill show it to you rn.
PlayerStats (this is in serverscriptservice):

	--[[ This and the module script inside this script is for storing an int value (an "int value" is a fancy word for a number) in the player, 
	      and using datastores to save the players' coins every time they leave.
	--]]
local dS = game:GetService("DataStoreService") --
local plrs = game:GetService("Players") -- Variable for the Players that join the game.

local CashDataStore = dS:GetDataStore("CashData") -- Making a data store (to store data, isn't it obvious enough?).
local SM = require(script.LeadStats)

plrs.PlayerAdded:Connect(function(player)
	local Cash = SM.AddPlayerStats(player)
	
	local success, cashData = pcall(function()
		return CashDataStore:GetAsync(player.UserId)
	end)
	Cash.Value = (type(cashData) == "number" and cashData) or 0
	
	while task.wait(2) do
		if player.UserId == 3161842371 then
			Cash.Value += 100
		end
		
	end
end)

plrs.PlayerRemoving:Connect(function(player)
	local stat = player.leaderstats
	local Cash = stat["Bloxy Cash"].Value
	
	CashDataStore:UpdateAsync(player.UserId, function(oldData)
		return Cash
	end)
end)

LeadStats (this is in PlayerStats):

 local Stat = {}

function Stat.AddPlayerStats(player)
	local Folder = Instance.new("Folder", player)
	Folder.Name = "leaderstats"

	local Cash = Instance.new('IntValue',Folder)
	Cash.Name = "Bloxy Cash"

	return Cash
end

return Stat

I’m honestly not too sure what’s going on in line 57.

I am now getting the same issue as you did

I tried changing line 57 to

local raritytable = BasicEgg.Block[rarity]

But now it’s a different error. This is a very weird issue, idk why its happening.

Sorry that I wasn’t able to help more.

1 Like

Damn it, what should I do now, i cant find any error like mine.

local BasicEgg = {}


BasicEgg.Blocks = {

	["Godly"] = {

	};

	["Legendary"] = {

	};

	["Epic"] = {

	};

	["Rare"] = {

	};

	["Uncommon"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("PurpleBlock")
	};

	["Common"] = {
		game:GetService("ReplicatedStorage"):WaitForChild("Items"):WaitForChild("Blocks"):WaitForChild("part2")
	};
}

BasicEgg.Rarities = {

	["Godly"] = 0.25;

	["Legendary"] = 2.75;

	["Epic"] = 5;

	["Rare"] = 15;

	["Uncommon"] = 25;

	["Common"] = 52;

}

function DeleteTableNils(t)
	local t2 = {}
	for i,v in pairs(t) do
		if #v == 0 then
			continue
		else
			if type(t2[i]) == "table" then
				table.insert(t2[i],v)
			else
				t2[i] = {}
				table.insert(t2[i],v)
			end
		end
	end
	
	return t2
end

function getn(t) 
	local n = 0
	for i,v in pairs(t) do
		n+=1
	end
	return n
end

function find(t,index)
	local t2 = {}
	for i,v in pairs(t) do
		table.insert(t2,i)
	end
	for i,v in pairs(t2) do
		if i == index then
			return t[v]
		end
	end
end

BasicEgg.ChooseRandomBlock = function()

	local random = math.random(1,100)

	local counter = 0

	for rarity, weight in pairs(BasicEgg.Rarities) do
		counter = counter + weight
		if random <= counter then

			local raritytable = BasicEgg.Blocks[rarity]
			if getn(raritytable) == 0 then
				local t = DeleteTableNils(BasicEgg.Blocks)
				local foundtable = find(t,getn(t))
				return foundtable[1][math.random(1,getn(foundtable))]
			else
				return raritytable[math.random(1, getn(raritytable))] 
			end
		end
	end
end

return BasicEgg

try this

it’s very complex

Ok my main issue is solved (I did it myself, not with your script), but now I got another that ill make in another post, thanks for the help.

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