Simple Item Buy Module Script [Open-Sourced][NEW UPDATE]

Hello Developers i Hope You Nice Day Today I Have Module Script let you Call Function To Buy item From One Line lets Get started Tutorials Firstly Create Module Script ReplicatedStorage Rename It what you went, and we put in It 2 table first one store RemoteEvents to use it in any script and second one store items or tools you went player buy it So our module script will be like that :

local buy = {}

buy.Events = {-- Here We Make Table That Can Store Remote Events To put it In Function Example buy.Events[1], buy.Events[2]
	game.ReplicatedStorage.item1,
	game.ReplicatedStorage.item2,
    --You Can add More	
}

buy.Items = {-- Here We Make Table That Can Store Tools To put it In Function Example buy.Items[1], buy.Items[2]
	game.ReplicatedStorage.Items.ClassicSword,
	game.ReplicatedStorage.Items.Torch,
	--You Can Add More
}

Now after we add Table lets go to main part in module Lets Make functions
first function this its not necessary to use it but i use it to fire server and add to it remote event Table :

function buy:FireServer(event)-- FireServer Events It is not necessary to use it
	event:FireServer()
end

now lets make function that check if player have enough money to buy item and put some variable in this function:

function buy:Cash(item , price, plr)-- This Function Check If Player Have Enough Cash To Buy Item
	if plr.leaderstats.Cash.Value >= price then
		local item2 = item:Clone()
		item2.Parent = plr.Backpack
		plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - price
	else
		print("You Dont Have Enough Cash Bro")
	end
end

Now After add this function lets add function that check if remote event get fired from client to server :

function buy:EventCash(Event, item, price)-- This Function Called In  ServerScriptService When Player Fire Server To Buy Tool You call It Like That module:EventCash(module.Events[number of remoteEvent In table],module.Items[number of Tool in Table],Here Price Of Item)
	Event.OnServerEvent:Connect(function(plr)
		buy:Cash(item, price, plr)
	end)
end

Just when call this function add remote event and item and price
now our module now Become complete
Here :

local buy = {}

buy.Events = {-- Here We Make Table That Can Store Remote Events To put it In Function Example buy.Events[1], buy.Events[2]
	game.ReplicatedStorage.item1,
	game.ReplicatedStorage.item2,
    --You Can add More	
}

buy.Items = {-- Here We Make Table That Can Store Tools To put it In Function Example buy.Items[1], buy.Items[2]
	game.ReplicatedStorage.Items.ClassicSword,
	game.ReplicatedStorage.Items.Torch,
	--You Can Add More
}



function buy:FireServer(event)-- FireServer Events It is not necessary to use it
	event:FireServer()
end


function buy:Cash(item , price, plr)-- This Function Check If Player Have Enough Cash To Buy Item
	if plr.leaderstats.Cash.Value >= price then
		local item2 = item:Clone()
		item2.Parent = plr.Backpack
		plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - price
	else
		print("You Dont Have Enough Cash Bro")
	end
end

function buy:EventCash(Event, item, price)-- This Function Called In  ServerScriptService When Player Fire Server To Buy Tool You call It Like That module:EventCash(module.Events[number of remoteEvent In table],module.Items[number of Tool in Table],Here Price Of Item)
	Event.OnServerEvent:Connect(function(plr)
		buy:Cash(item, price, plr)
	end)
end

Note: To Add More Values Just Repeat the same step But With buy:Cash() And buy:EventCash()

Now Lets Use Module And Call Function Firstly add Remote events in ReplicatedStorage And put place Of Remote in module table After add it make button and insert local Script :

local object = require(game.ReplicatedStorage.ProcessItem)

script.Parent.MouseButton1Up:Connect(function()

object:FireServer(object.Events[1])

end)

After We Fire Server And Sent Massage Lets Add Script in ServerScriptService

Script:

local object = require(game.ReplicatedStorage.ProcessItem)

object:EventCash(object.Events[1],object.Items[1], 300)--Function Buy Item

Here Test:

Here Model :

Here Place To Test:

ItemBuy ModuleScript.rbxl (38.6 KB)

i hope you guys this helped and say feedback about module and what add on it thank you for reading :slight_smile:

13 Likes

I like this. Simple, easy to use. This kind of stuff is perfect for begginners.

Thumbs up from me :+1:

1 Like

You should note that exploiters can change 300 to any negative number to give them that amount of money, instead you should subtract it on the server, not taking in any number from the client

1 Like

Yeah that what I did in client I sent massage only to server and in server check if player has price of item or not

Thanks bro I hope that helped :smile: :smile:

Hi! I’ve come to request a really good feature to the already good feature you already provided!

buy.Store = {
           ["Sword"] = {
                        ["Item"] = game.ReplicatedStorage.Sword
                        ["Price"] = 300
                       }
            }

function buy:Item(item, cashSource, plr) -- we do cashsource instead of leaderstats since not everyone uses leaderstats
   if cashSource:IsA("IntValue") or cashSource:IsA("NumberValue") then
         if cashSource.Value >= buy.Store[item]["Price"] then
                local tool = buy.Store[item]["Item"]:Clone()
                tool.Parent = plr.Backpack
                cashSource.Value -= buy.Store[item]["Price"] -- minus equals is the shortcut and wont fire any errors.
        end
   end    
end
--Use:
--pretend that the module is required
buy:Item("Sword", plr.Money, plr)
--Instead of the previous method where you need to worry about [1] <-- index
Also does the event cash automatically since it is fired from the server.

This can help the simplicity of not always inputting the price everytime you need it, instead just do this. Anyways! Great module! Will be waiting for more updates to this! This really helps new programmers :slight_smile:

Depricate EventCash since it is very exploitable. It will also make it slower and less performant. Depricate a.k.a remove. All you need is buy:Item() tbh…

2 Likes

I dont think its necesarily used on the client. FireServer should be depricated.

1 Like

Wow nice Function and nice table now I added this in module very nice bro​:+1::clap:

I will remove FireServer Soon and add new functions

UPDATE:

Now I Updated Module And i Put Function And This Function Idea From @123dsadqwe special thanks to him Update Log

  1. Remove Useless functions Like FireServer(),EventCash()

  2. Added function That Can Put :Item("Sword",plr.leaderstats.Cash, plr)Without Put Price On This Function Because Will Store Price Look In Point 3 :point_down:

  3. Added Table that Store Items With informations Like Place Of Item And Price Example :
    buy.Store = { ["Sword"] = { ["Items"] = game.ReplicatedStorage.Items.ClassicSword, ["Price"] = 300 }, } look it at module to understand it

  4. Added Functions That Increase(plr.leaderstats.Cash, 11000) Increase leaderstats of Player Just put Source Of money And Value you Went Increase

Here Module :point_down: :

https://www.roblox.com/library/6495980867/ProcessItem

And special thanks For @123dsadqwe to suggest this function , Table to me he helped so much enjoy :slight_smile:

2 Likes