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