Need help on Module

So I have this module script and I want it to choose either one of these values but it only picks one. Please help!

local plr = game.Players.LocalPlayer
local rs = game.ReplicatedStorage

local SendCoin = rs:WaitForChild("SendCoin")

local CoinGiver = {
	["Small_Coin"] = {
		["Value"] = 1
		
	},
	
	["Bundle_Coin"] ={
		["Value"] = 3
		
	}
	
}

local CoinGUI = plr.PlayerGui.CoinGui

local CoinButton = CoinGUI.CoinButton

local Rolls = 0


local module = {}


function module.ChooseCoinsValue(Coin)
	local function Random_Coin()
		
		local Chance = 1
		local Value_Coin = 0
		if CoinGiver[Coin] then
			if Rolls == 0 then
				Rolls += 1
				Value_Coin = CoinGiver[Coin].Value
				SendCoin:FireServer(Value_Coin)
					
				end
			end
			task.wait(.3)
			Rolls = 0
		end
		CoinButton.MouseButton1Down:Connect(Random_Coin)
	end

return module

I still need help. I cant get it to work

not really sure what’s going on here (i also barely looked)

local function GetRandomCoin()
  return math.random(0, 1) == 1 and Coins["Small_Coin"] or Coins["Bundle_Coin"]
end

I would prefer not using “or” because its inefficient.

it’s not inefficient at all
did someone say it was?

-- It would take slightly longer to write this over the 1 liner
if math.random(0, 1) == 1 then
  return Coins["Small_Coin"]
else
  return Coins["Bundle_Coin"]
end
return math.random(0, 1) == 1 and Coins["Small_Coin"] or Coins["Bundle_Coin"]

these two do the exact same thing but 1 is a lot shorter and it’s not hard to understand at all

Ill be adding more coins which Ill have to add more “or” statements which makes it inefficient. I need a system that automatically handles that.

then you’d need to get all of the indexes to pick a random coin
you could use a function to get the indexes

function indexes(t}): {any} -- I would call it table.keys if i could
  local pile = {}
  for i in t do
    table.insert(pile, i)
  end
  return pile
end

local function GetRandomCoin()
  local CoinTypes = indexes(Coins)
  return Coins[CoinTypes[math.random(#CoinTypes)]]
end

I see, where would I place this?

i’d make it a module function

local Module = {}

function indexes(t)
	local pile = {}
	for i in t do
		table.insert(pile, i)
	end
	return pile
end

function Module:GetRandomCoin(Coin)
	local CoinTypes = indexes(Coins)
	return Coins[CoinTypes[math.random(#CoinTypes)]]
end

return Module

handle the button clicking outside of the module

It doesn’t give me any coins, here’s the script.

local plr = game.Players.LocalPlayer
local ts = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local rs = game.ReplicatedStorage

local SendCoin = rs:WaitForChild("SendCoin")

local CoinGiver = {
	["Small_Coin"] = {
		["Value"] = 1,
		["Image"] = "15539383618"
	},
	
	["Bundle_Coin"] ={
		["Value"] = 3,
		["Image"] = "15539386102"
	}
	
}

local CoinGUI = plr.PlayerGui.CoinGui

local CoinButton = CoinGUI.CoinButton

local Rolls = 0


local module = {}

function Indexes(t): {any}
	local pile = {}

	for i in t do
		table.insert(pile,i)


	end
	return pile
end

function module.ChooseCoinsValue(Coin)
	local function Random_Coin()
		local Image_Coin = script:WaitForChild("Coin"):Clone()
		local Chance = 1
		local Value_Coin = 0
		if CoinGiver[Coin] then
			if Rolls == 0 then
				local CoinTypes = Indexes(Coin)
				
				Rolls += 1
				Value_Coin = CoinGiver[Coin].Value
				SendCoin:FireServer(Value_Coin)
				Image_Coin.Image = "rbxassetid://"..CoinGiver[Coin].Image
				Image_Coin.Position = UDim2.new(math.random(10,80)/100, math.random(10,80), math.random(10,100)/100, math.random(10,100))
				Image_Coin.Parent = CoinGUI
					
					
					local Delete_Coins = coroutine.create(function()
						Image_Coin:Destroy()
					end)
					
					while task.wait(2) do
					coroutine.resume(Delete_Coins)
			
					end
					return Coin[CoinTypes[math.random(#CoinTypes)]]
				end
			end
			task.wait(.3)
			Rolls = 0
			CoinButton.MouseButton1Down:Connect(Random_Coin)
		end
		
	end

return module

probably because you aren’t using your module properly

-- This should be a LocalScript in the CoinButton
-- You have a ModuleScript so use it properly
local RS = game:GetService("ReplicatedStorage")
-- No reason to make TweenInfo variable if you're using default arguments

local Player = game.Players.LocalPlayer
local SendCoin = RS:WaitForChild("SendCoin")

local CoinInfo = require(--[[ModuleScript]])
local CoinGUI = script:FindFirstAncestor("CoinGui") -- Assuming CoinGui is an ancestor
local CoinButton = script.Parent

local Coin = script:WaitForChild("Coin")
Rolls = 0

CoinButton.MouseButton1Down:Connect(function()
  local CoinClone = Coin:Clone()
  local Chance = 1
	
  if Rolls ~= 0 then return end
  local RandomCoin = CoinInfo:GetRandomCoin()
	
  Rolls += 1
  SendCoin:FireServer(RandomCoin.Value)
	
  CoinClone.Image = "rbxassetid://".. RandomCoin.Image
  CoinClone.Position = UDim2.new(
    math.random(10, 80) / 100, 
    math.random(10, 80), 
    math.random(10, 100) / 100, 
    math.random(10, 100)
  )
  CoinClone.Parent = CoinGUI
	
  task.wait(2)
  CoinClone:Destroy()
  task.wait(0.3)
  Rolls = 0
end)
local CoinInfo, Coins = {}, {
	["Small_Coin"] = {["Value"] = 1, ["Image"] = "15539383618"};
	["Bundle_Coin"] = {["Value"] = 3, ["Image"] = "15539386102"};
}

-- CoinInfo and Coins could be in the same table if you wanted

function Indexes(t)
	local pile = {}
	for i in t do
		table.insert(pile, i)
	end
	return pile
end

function CoinInfo:GetRandomCoin(Coin)
	local CoinTypes = Indexes(Coins)
	return Coins[CoinTypes[math.random(#CoinTypes)]]
end

return CoinInfo

Why function in function ;-;

Charchar

Calling functions within functions are a very common programming practice. It’s the basis of all recursion, which is a major part of programming logic.

Consider an implementation of the factorial function. If you are not aware, the factorial of an integer x is equal to x * (x - 1)!, where x! is equal to x * (x - 1) * (x - 2) * ... * (x - n) for x > 0 and 1 when x = 0.

function factorial(x)
  if x == 0 then return 1 end
  return x * factorial(x - 1)
end

The entire basis of the function relies on the fact that the factorial can be defined in terms of itself; thus, calling functions within functions. Better get used to it :stuck_out_tongue:

No no not like that lol that’s recursion not function in function lol

This is what’s i meant:

local function Foo() 
 local function Bar() 
 end
end

Let me give you an example. If a module does multiple functions, such as head tilt and camera tilt.

local RunService = game:GetService("RunService")
local HeadTiltEnabled = true
local CameraTiltEnabled = false

local HEADTILT,CAMERATILT

local HeadTilt = function ()
    local Action_HeadTilt = function()
        --performs headtilt--
    end

    HEADTILT = RunService.RenderStepped:Connect(function()
        task.defer(Action_HeadTilt)
    end
end

local CameraTilt = function ()
    local Action_CameraTilt = function()
        --performs cameratilt--
    end

    CAMERATILT = RunService.RenderStepped:Connect(function()
        task.defer(Action_CameraTilt)
    end
end

module.Init = function ()
    HEADTILT:Disconnect()
    CAMERATILT:Disconnect ()
    
    if HeadTiltEnabled then HeadTilt() end
    if CameraTiltEnabled then CameraTilt() end
end

You know it’s not quite practical to put headtilt and cameratilt together, but just as an example, Action_Headtilt is only required by HeadTilt, similarly Action_CameraTilt is only required by CameraTilt. If I put them out of the main function, it will stay there as a basically global scope variable that won’t be garbage collected, which might cause memory leak. Putting them inside their main function will cause them to be cleaned after that function and connection, thus making the code more organised, clean and also make it easier to handle further problems.

Hope this cleaned something up for you.

Did you read the original code of the thread owner?

Which part of it? Honestly I think it’s quite decent,

it connects to the local function at last of it, which is actually preferred more than just doing

Event:Connect:(function()
    --all the stuff
end)

It’s all about feeling what looks cleaner to you and more organised.

Though it actually should connect out of the local function but in the module function.

Alright, thank you, I’ll be trying it soon!

Worked like a charm! I just edited your script a bit.

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