Requiring module throws an error

I am trying to make a crouch system using modules. The reason why I’m using modules is so that I can know if the player is crouching or not.

local util = {}

util.Status = {
isCrouching = false;
isSafe = false;
}

return util

When the player enter a safe zone, it sets the isSafe value to true by requiring the module of the player which touched the zone. But when I do this, it throws an error saying “Requested module experienced an error while loading”. Here’s my safe zone code

for i, v in ipairs(script.Parent:GetChildren()) do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				local util = require(plr:WaitForChild("Util"))
				util.Status[2] = true
				print(util.Status[2])
			end
		end
	end)

	v.TouchEnded:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				local util = require(plr:WaitForChild("Util"))
				util.Status[2] = false
				print(util.Status[2])
			end
		end
	end)
end

Help would be appreciated :))

1 Like

You are requiring it twice, try having a variable for it other than that…?

1 Like

Nope, it doesn’t work. It throws the same error again.

1 Like

Are you sure that the Util module exists inside the player instance NOT the character / somewhere else inside the player not the actual instance like a descendant ?

1 Like

Yes, it exists within the player, not the character.

game.Players.PlayerAdded:Connect(function(plr)
	local mod = script.Util:Clone()
	mod.Parent = plr
end)
1 Like

You said the error was “Requested module experienced an error while loading” this means that there was something wrong with the module itself not loading it, are you sure the module itself has no errors? Try requiring it from the command bar and see if it did the same error (without starting the game) if yes than the error is the module itself.

YEAH, something’s wrong with the module.
Can you help me figure out what’s wrong in the module?

local mod = {}
local ts = game:GetService("TweenService")
local hum : Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local cc = workspace.CurrentCamera

mod.Status = {isCrouching = false; isSprinting = false; isSafe = false};
	
function mod:Crouch(
	crouchVal : boolean, 
	crouchSpeed : number, 
	normSpeed : number,
	crouchTweenParams,
	normTweenParams,
	crouchCameraParams,
	normCameraParams,
	tweenTime : number
)
	mod.Status[2] = false;
	if crouchVal then
		mod.Status[1] = true;
		hum.WalkSpeed = crouchSpeed;
		ts:Create(cc, TweenInfo.new(tweenTime), crouchCameraParams):Play();
		ts:Create(hum, TweenInfo.new(tweenTime), crouchTweenParams):Play();
	else
		mod.Status[1] = false;
		hum.WalkSpeed = normSpeed;
		ts:Create(cc, TweenInfo.new(tweenTime), normCameraParams):Play();
		ts:Create(hum, TweenInfo.new(tweenTime), normTweenParams):Play();
	end;
end

return mod


here, you can’t require this with a server script, if you want to, remove this variable and have it as a parameter for the function needed for it.

2 Likes

ty so much man, really appreciate it :smile:

1 Like

Can I ask another question?? ShouldI use region3 over the touched event?

no, you should use the touched event or Zone+ (a very popular open-sourced module)

Thanks for the help man! awesome!

1 Like

How about if there is no error but when you play the game it has this error?

This means that something changes the content of the module script before you require it, try using the command bar and calling each function inside the module to make sure it doesn’t error.

Well, everything seems to work fine. There is no error found.

Can u send me a picture of the error, the module script and the script which requires it?