Help with variables in module script

I Have a module script with variables shared across functions. I want these variables to be accessed by both functions but for individual players (because more then one person will use this module at a time). The issue is that since this module is accessed by both a server end and client end it gets messy with variables. More or less my problem is that I don’t know the right way to use my variables since I’ve barely used module scripts before. I’ve kind of written myself into a hole, but this function has to run through a module.
thanks everyone for reading and looking through my messy code lol.

Here is how I wrote my module:


local module = {}

Animation = script.Animation
local Anim, Anim2
local a 
local rushing = false
local TimeSinceStart,readytostop,readytostop2

function module:Fire(server,Script,Player,r)
	if server then
		readytostop = false
		wait(.2)
		Anim = Player.Character.Controller:LoadAnimation(Animation)
		Anim:Play()
		readytostop = true
		
	else
		readytostop2 = false
		TimeSinceStart = time()
		rushing = true
		wait(.2)
		a = Player.Character.RightLowerArm.Touched:connect(function(h)
			-- just some damage stuff
		end)
		readytostop2 = true
		repeat wait(.01)
			if (time() - TimeSinceStart) > 6 then
				rushing = false
				a:Disconnect()
				r:FireServer("EndForce") -- this forces the server to run module:End()
				print("Tried to stop")
				break
			end	
		until rushing == false 
		
	end
end

function module:End(server,Script,Player,r)
	
	if server then
		repeat wait() until	readytostop == true
		Anim:Stop()
	else
		repeat wait() until	readytostop2 == true
		rushing = false
		a:Disconnect()
	end
	
end
return module

im confused about your question. Are you trying to make a new module for each player so that they each have there own set of variables?

Im using one module on the server and I want each player to have their own set of variables. (I don’t really want to make a new module for each player just because of how my game is set up right now, but I could do that). I’m just asking if there’s another way to do the same thing.

usually OOP and metatables would be perfect as they allow for indvidual objects with a single module. To make it easier for you though you can simply make a table inside the module with a playername as a key which you can then store the variables for each player in there. Then, if you want to access certain variables simply get the table and pass the playername.

cant believe I didn’t think of that. thanks for the quick solution!

1 Like