This post was and has been solved long ago.
Okay listen up, in the script, you can get rid of the data.player
assignment and use the plr
parameter that is passed into the function. This way, you don’t have to set the data.player
variable every time the function is called.
You can also eliminate the data.myScraps
and data.remoteArg
assignments and pass those values directly into the addItem
function. This way, you don’t have to set those variables every time the function is called.
In the module script, you can also get rid of the module.player
, module.myScraps
, and module.remoteArg
variables and instead pass those values directly into the addItem
function as parameters. This way, you don’t have to use global variables to store those values.
@xDeltaXen is right; you can just do something such as function module.addItem(name, price, player)
and get the data to use inside the function from the player argument. That answers your question, that’s more efficient. Also, this way it’s definitely safer.
What I’m wondering is if this even works, since you’re not actually changing the player’s data after they buy the item - you’re just changing the module.myScraps value, but not the value inside of the actual ValueObject.
Security is also a flaw here, but tell me if you care about that right now and want me to point it out.
I have fixed this, thanks for telling me though.
I see what you’re trying to do. One way to make this more efficient would be to pass the player object and the item name as arguments to the addItem
function, rather than storing them as module variables. This way, you don’t have to set the player and item as module variables before calling addItem
. Here’s an example of how you could modify your code to do this
-- SCRIPT
local data = require(game.ReplicatedStorage.Modules.ModuleScript)
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,item)
-- Pass player and item as arguments to addItem
data.addItem(plr, item)
end)
-- MODULE SCRIPT
local module = {}
function module.addItem(plr, item)
local myScraps = plr.Data.Scraps.Value
if item == "exampletool1" and myScraps >= 5 then
myScraps -= 5
local clone = game.ReplicatedStorage.PurchaseAble[item]:Clone()
clone.Parent = plr.Backpack
end
end
return module
This way, you can call addItem directly with the player and item arguments, rather than setting them as module variables first.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.