OOP values updating for all players

Hello all,

I’m sort of new to OOP with Lua and was messing around with OOP using values in a character. Using a players mouse they could change certain values (you can see in the module script). It works great with one player, but when I go to two or more players the script updates the values for all players.

No errors in output or anything.

Only other post I could find with a related issue is this one.

Module Script:

local ControllerFramework = {}
ControllerFramework.__index = ControllerFramework


function ControllerFramework.new(Player, Team)
	local self = setmetatable({}, ControllerFramework)
	self.Player = Player
	self.NPCCheck = Player.Character.Values.ClickedOnNPCCheck.Value
	self.NPCObject = Player.Character.Values.ClickedOnNPCObject.Value
	self.Team = Team
	
	return self
end

function ControllerFramework:MouseClicked(MouseTarget)
	print(self.Player)
	if MouseTarget ~= nil then
		if MouseTarget.Parent:FindFirstChild("Humanoid") and not self.NPCCheck then
			self.NPCCheck = true
			self.NPCObject = MouseTarget.Parent
			print("ClickedOnNPC")
		elseif MouseTarget.Parent:FindFirstChild("Humanoid") and self.NPCCheck and self.NPCObject == MouseTarget.Parent then
			print("Unclicked on NPC")
			self.NPCCheck = false
			self.NPCObject = nil
		elseif MouseTarget.Parent:FindFirstChild("Humanoid") then
			self.NPCObject = MouseTarget.Parent
			print("ClickedOnAnotherNPC")
		else
			print(MouseTarget)

		end
	end
end


return ControllerFramework

Script (I’m not sure if the error is in here):

local RemoteFunc = game.ReplicatedStorage.Events.RemoteFunction
local RemoteController = require(game.ServerScriptService.ModuleScript)

local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local Gun = RemoteController.new(Player)


RemoteFunc.OnServerInvoke = function(Player, MouseClickedPosition)	
	print(Gun)
	
	Gun:MouseClicked(MouseClickedPosition)
end

You seem to be triggering the method for all players?

Gun is dynamic and since this code seems to be linked to StarterCharacterScripts mean which data table it’s linked to changes for each user. However you call the method assuming one person triggered it and don’t check on who actually triggered it.

Thank you!

What/how could I implement or change my code? Like change the remote function to a remote event? or something like change the server scripts parent to not be in StarterCharacterScripts?

I would recommend to adapt the code to not be in StarterCharacterScripts and ideally get the player’s data table through the Player argument passed through the ServerInvoke.