Script passing messed up values to module

I’m trying to pass some parameters through a script to a modulescript, although it’ll destroy the params value and replace the plr value with the original params value
Script:

game.ReplicatedStorage.Remotes.Admin.OnServerEvent:Connect(function(plr, command, params)
	if table.find(UIds, plr.UserId) and commands[command] then
		commands[command](plr, params)
	end
end)

Module:

local datastore = require(game.ServerStorage.Modules.DataStore)
local command = {}

function command:gcash(plr, params)
	local pastdata = datastore:GetPlayerData(plr)
	local success, err = pcall(function()
		datastore:UpdateAsync(plr.UserId, function(pastdata)
			pastdata["Credits"] += params[1]
			return pastdata
		end)
	end)
end

2 Likes

This is a basic example of how you could pass data to a ModuleScript:

Module:

local Module = {}

function Module.Print(Argument)
    print(Argument)
end

return Module

Script:

local Module = require(script.Module)
Module.Print("Hello World!")

It does pass the data, although I’m trying to figure out why it passes the wrong data
this is what the script prints:
image
this is what the module prints:
image
It should be passing the same exact values, although it doesn’t

There’s a difference when defining functions within tables/modules.

If you define the function with a dot:

function module.foo(argument)
    print(argument)
end

module.foo("Something") -- Prints "Something"

but when defining the function with a colon:

function module:foo(argument)
    print(argument)
end

module.foo("Something") -- Prints "nil"
module:foo("Something") -- Prints "Something"
module["foo"]("Something") -- Prints "nil"

This is due to Lua’s syntax, when you define the function with a colon, the first passed variable is ‘self’ and depending on how you call the function defines which is the first passed variable. Calling a function with a colon makes the first passed variable itself which should be the table the function is defined in.

To see more in detail what self is.

Thanks! Been struggling with this problem for some time

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