So here is my module script and I'm trying to add coins to a datastore ok
CoinData = game:GetService('DataStoreService'):GetDataStore('Coins') function AddCoins(plr, toadd) print(plr) if CoinData:GetAsync(plr) then CoinData:IncrecementAsync(plr, toadd) print(CoinData:GetAsync(plr)) end end return AddCoins
But when I call the function from a script..
a = require(game.ServerScriptService.AddCoins)() AddCoins('Trolna', 100)
I get slapped with
08:30:39.775 - Argument 1 missing or nil 08:30:39.775 - Stack Begin 08:30:39.775 - Script 'ServerScriptService.AddCoins', Line 4 08:30:39.776 - Script 'a = require(game.ServerScriptService.AddCoins)() AddCoins('Trolna', 100)', Line 1 08:30:39.776 - Stack End
halp
The first thing I notice is the fact that you are calling the function as soon as you get it from the module script without any arguments. This is most likely the reason you are getting the error. Do something like this instead:
AddCoinFunc = require(game.ServerScriptService.AddCoins) AddCoinFunc('Trolna' , 100)
Another thing I noticed is the fact that you are using the player name. This is very dangerous and should never be used because players have the ability to change their name. Use their player ID instead which is a constant unique value attributed to every player. Finally, your call to the function should be wrapped in a pcall()
function in order to catch any error Data stores always have the possibility of error because it has to make web calls to the Roblox servers. Your final scripts would look like this:
Module Script:
CoinData = game:GetService('DataStoreService'):GetDataStore('Coins') function AddCoins(plrID, toadd) print(plrID) if CoinData:GetAsync(plrID) then CoinData:IncrecementAsync(plrID, toadd) print(CoinData:GetAsync(plrID)) end end return AddCoins
Script:
AddCoinFunc = require(game.ServerScriptService.AddCoins) local success = pcall(function () return AddCoinFunc(Trolna.PlayerID , 100) end) if not success then --handle case where data store errors end
Module script:
local module={} CoinData = game:GetService('DataStoreService'):GetDataStore('Coins') function module.AddCoins(plr, toadd) print(plr) if CoinData:GetAsync(plr) then CoinData:IncrecementAsync(plr, toadd) print(CoinData:GetAsync(plr)) end end return module
Other script:
a = require(game.ServerScriptService.AddCoins).AddCoins('Trolna', 100)