ModuleScript not working no errors

There are no errors could someone help me fix this??

–modulescript

local module = {}
module.__index = module

function module:settableandclasses(part,player,distance)
	local hahatable = setmetatable({},self)
	
	hahatable.Part = part
	hahatable.Player = player
	hahatable.Distance = distance
	
	return hahatable
end

function module:Detectandhurtplayer()
	local plr = self.Player
	local part = self.Part
	local dist = self.Distance
	
	local magnintude = (part.Position-plr.HumanoidRootPart.Position)
	if magnintude < 5 then
		local hum = plr:FindFirstChildOfClass'Humanoid'
		hum:TakeDamage(math.random(1,10))
		print(plr.Name.."'s Humanoid damaged")
	end
	
end

function module.__add(thistable,val)
	thistable:settableandclasses()
	thistable:Detectandhurtplayer()
end

return module

– requiring script

local folder = script.Parent

local module = require(workspace.ModuleScript)

local distance = 5

local players = game:GetService("Players"):GetPlayers()

for _, player_i in pairs(players) do
	if not (player_i.Character)then
		continue	
	end
	
	for _, player in pairs(players) do
		if player_i==player or not player.Character then
			continue
	end
	
for i,part in ipairs (folder:GetChildren()) do
	if part then
		continue
		end

			 module:settableandclasses(part,player,distance)
			
			
		end	
	end
end

is this not supposed to be if not part then?

local magnintude = (part.Position-plr.HumanoidRootPart.Position).magnitude

It still doesnt work. and no errors

If anyone’s having the same issue, we ended up working it out in private messages.
It turns out that the for loop that goes through the players wasn’t running at all because it only ran at the beginning of the game. Fixed that by putting the code in a while loop. There were a few other small issues such as changing ply.Humanoid to ply.Character.Humanoid and using .magnitude like csqrl suggested.

Without the module script, the code to damage the player when they get close to a part ended up looking like this:

local folder = script.Parent
local distance = 5

function checkIfCloseToPart(player)	
	for _, child in pairs(folder:GetChildren())do
		if child:IsA("BasePart") then
			local magnitude = (child.Position-player.Character.HumanoidRootPart.Position).magnitude
			
			if magnitude < 5 then
				return true
			end
		end
	end	
	
	return false
end

function damagePlayer(player, damageAmount)
	local hum = player.Character:FindFirstChildOfClass('Humanoid')
	hum:TakeDamage(damageAmount)
	print(player.Name.." took " .. damageAmount .. " damage.")
end

while true do
	wait(1)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if not player.Character then continue end
		local isCloseToPart = checkIfCloseToPart(player)
		
		if isCloseToPart then
			local damageAmount = math.random(1,10)
			damagePlayer(player, damageAmount)
		end
	end
end

OP wanted to use OOP though so we had to figure out why the module script wasn’t working.
It turns out __add wasn’t firing in his module script. __add only runs when you use the + operator on the metatable
.
We found this example helpful to figure out how to use it and get the __add function running.

We also ended up restructuring the code to like this instead of using __add: All about Object Oriented Programming

Here is the demo place if you want to see the code / give it a try: demo.rbxl (19.7 KB)

1 Like