Trying to make a random fruit genorator

Can someone read my script and see what is wrong with it

local FruitToolFolder = RP.FruitTool
local Fruits = RP.Frutis
local RunService = game:GetService("RunService")
local partFolder = game.Workspace.PartHolder

local Cd = 10
local CurrentTime = os.clock()

local itemChances = {}

for i,v in pairs(Fruits:GetChildren()) do
	local curChance = v:GetAttribute("Chance")
	if curChance == Fruits:GetAttribute("Chance") then
		print(v.Name)
		itemChances[v.Name] = curChance
	end
end




local function GetItem()
	local weight = 0
	for _, chance in pairs(itemChances) do
		weight += chance
	end
	
	local randomNum = math.random(1,weight)
	
	
	weight = 0
	
	for prize, chance in pairs(itemChances) do
		weight += chance
		
		if weight >= randomNum then
			print("Spawning...",  prize)
			return prize
		end
	end
	
end



local function GetModel(itemName)
	
	for i,v in pairs(Fruits:GetChildren()) do
		if v.Name == itemName then
			return v
		end
	end
	
end

local function GetSpot()
	local randomLocation = math.random(1,#partFolder:GetChildren())
	
	return partFolder:GetChildren()[randomLocation]
end

local function SpawnItem()
	local itemName =  GetItem()
	local model = GetModel(itemName)
	local spot = GetSpot()
	
	
	if not model then return end
	
	local modelClone = model:Clone()
	modelClone.Parent = workspace.SpawnedItems
  	modelClone.PrimaryPart.CFrame = spot.CFrame
end



RunService.Heartbeat:Connect(function()
	local passedTime = os.clock() - CurrentTime
	
	if passedTime >= Cd then
		CurrentTime = os.clock()		
		print("Spawing Part")
		SpawnItem()	
	end
	
end)

And why do I get this error
image

1 Like

No need to say #partsfolder:GetChildren()

Just say #partsfolder

Can you do

print(weight)

before

local randomNum = math.random(1,weight)

to debug it?

I still get the same error when I try this

U cannot do that ,this is because your varaible has no list of numbers,u need to put it in a table.

Create a table.

When addind,add table.insert(the new weight)

The weight stays 0 that is what it said in the output

math.random(1,0) will yield the error then
Print the weight in the for loop as it’s being incremented to check that

Bruh,u didnt add table.insert(itemsChances, varaible)

1 Like

Here like this
image

Did it work???Can u help me check

I tried it but I get the same error
image

Make your weight a table like this ()

And put the 0 inside the table

When you mean make a table your talking about the varible that I added local weight = 0
and change it to a local weight = {}

1 Like

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