New to module scripts and i ran into a problem

local module = require(game.ServerScriptService.moduledie)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:connect(function(char)
			module(char)
	end)
end)

the module script is supposed to kill the player
and it doesnt for some reason… any help?

1 Like

You need to reference a function in the module script. With that you are just trying to make the main module inside the module script a function, and putting character in it’s arguments. You need to do something like module.killfunction(char) or whatever the path to your kill function is.

Can you show us what the module script looks like? Pretty hard to debug without the rest of it.

What’s the script inside the /game/ServerScriptService/moduledie and how it doesn’t kill the player? What error does it return, etc? You need to be more specific.

Also I’d avoid using char as a variable name only because in many programming langauges like C++, it’s a keyword for the character data type.

That ModuleScript could return a function. How do you know that returns a dictionary if the OP isn’t specific about what does the ModuleScript return?

2 Likes
local module = {}
local players = game:GetService("Players")
function die(plr)
   plr.Character:BreakJoints()
end

return module

His module probably returns a function, if ot has one function it is better to just return that function

1 Like

You’re returning an empty table. Try return the function die instead.

return function(plr)
   repeat wait() until plr.Character
   plr.Character:BreakJoints()
end

Use plr.CharacterAdded:Wait() instead as apparently using repeat wait() until isn’t a good practice.
Something like this.

return function(plr)
    (plr.Character or plr.CharacterAdded:Wait()):BreakJoints();
end;
1 Like

You’re returning the variable ‘module’ which is defined as a table with no indices/values. You need to return the function ‘die’.

I’m not sure why you would want to return a singular function as simple as that from a module though, unless you’re using that in several scripts across the game. You’d be better off using that module as a util module of code you need to reuse across your game, so using the ‘module’ variable so you can expand its functionality later, you would do the following:

local module = {}
local players = game:GetService("Players")

function module.die(plr) --> now it's a child of the table 'module'
   plr.Character:BreakJoints()
end

function module.damage(plr, val) --> Just to show you how to add another function
	val = val or 0 --> make sure we have a damage value
	if plr and plr.Character then --> check if the player has a character (you might want to add this confirmatory statement to your 'die' function)
		local hum = plr.Character:FindFirstChildOfClass 'Humanoid' --> check to see if they have a humanoid
		if hum then
			hum.Health = hum.Health - val --> they have one, so let's remove the value
		end
	end
end

return module

Then to use it using your code above:

local module = require(game.ServerScriptService.moduledie)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:connect(function(char)
			module.damage(plr, 50)
			module.die(plr) --> you set the argument here to 'char', but in reality your module code requires a player instance as you index it's 'character'
	end)
end)

Also, check out @Blockzez implementation to determine whether there is a character to break, and to wait until there is one - this is an alternative to the ‘if plr.Character’ statement which would activate the function when there is a character available, unlike my example above which would not fire if there wasn’t a character at that particular frame.

2 Likes