Updating a variable between multiple module scripts

I have 3 module scripts and one of them stores a variable that’s a vector. The other 2 update that vector when they are activated. I have a problem that the variable isn’t updated for the other 2 scripts. For example, the vector in the script that stores the variable would be 2,2,8, the second script would be 2,2,5 and the third one would be 2,2,10. How would I make it so it updates between multiple module scripts?

1 Like

Store an IntValue or NumberValue in the workspace and update that, then reference it in all 3 scripts.

1 Like

Modules are fine for holding values (but they do NOT cross the client-server boundary).

Also you should probably return the vector in a table, since returning a raw Vector3 returns different copies of it for each script that references it. This has the added bonus of using metatables to track changes made to the Vector3s.

Can you send your code?

local CameraVariables = {}

CameraVariables.cameraAngleX = 0
CameraVariables.cameraAngleY = 0

local intOffset = script.Parent.CameraOffset.Value

CameraVariables.cameraOffset = Vector3.new(2,2, intOffset)
CameraVariables.sensitivity = 0.1
CameraVariables.newOffset = nil

return CameraVariables

local Zoom = {}

local MIN_ZOOM = 1
local MAX_ZOOM = 20
local ZOOM_SPEED = 1

local cameraVariables = require(script.Parent.CameraVariables)

local cameraOffset = cameraVariables.cameraOffset
local newOffset = cameraVariables.newOffset

function Zoom.Zoom(a, b, input)
	print("a")
	local scrollDelta = input.Position.Z
	local zoomAmount = scrollDelta * ZOOM_SPEED
	local newZoom = math.clamp(cameraOffset.Z - zoomAmount, MIN_ZOOM, MAX_ZOOM)
	cameraOffset = Vector3.new(cameraOffset.X, cameraOffset.Y, newZoom)
	return cameraOffset
end

return Zoom

local NormalCamera = {}

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local player = Players.LocalPlayer

local MIN_ZOOM = 1
local MAX_ZOOM = 20
local ZOOM_SPEED = 1

camera.CameraType = Enum.CameraType.Scriptable

local isMouseButtonDown = false

local cameraVariables = require(script.Parent.CameraVariables)
local zoomModule = require(script.Parent.Zoom)

local Zoom = zoomModule.Zoom

local cameraOffset = cameraVariables.cameraOffset
local sensitivity = cameraVariables.sensitivity
local newOffset = cameraVariables.newOffset

local function BaseDefaultCamera(character)
	if not character then
		return
	end

	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")
	
	local function playerInput(actionName, inputState, inputObject)
		if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
			isMouseButtonDown = inputState == Enum.UserInputState.Begin
			if isMouseButtonDown then
				UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			else
				isMouseButtonDown = false
				UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			end
		end
	end

	RunService:BindToRenderStep("CameraUpdateN", Enum.RenderPriority.Camera.Value, function()
		if isMouseButtonDown then
			cameraVariables.cameraAngleX -= UserInputService:GetMouseDelta().X * sensitivity
			cameraVariables.cameraAngleY = math.clamp(cameraVariables.cameraAngleY - UserInputService:GetMouseDelta().Y * sensitivity, -75, 75)
		end

		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraVariables.cameraAngleX), 0) * CFrame.Angles(math.rad(cameraVariables.cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

		camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
		print(cameraOffset)
	end)
	
	ContextActionService:BindAction("ZoomNC", function(actionName, inputState, inputObject)
        if inputState == Enum.UserInputState.Change then
			cameraOffset = Zoom(nil, nil, inputObject)
        end
    end, false, Enum.UserInputType.MouseWheel)
	ContextActionService:BindAction("PlayerInputN", playerInput, false, Enum.UserInputType.MouseButton2, Enum.UserInputType.Touch)
end

function NormalCamera.NormalCamera()
	local character = player.Character
	if character then
		BaseDefaultCamera(character)
	else
		player.CharacterAdded:Connect(function(char)
			BaseDefaultCamera(char)
		end)
	end
end

function NormalCamera.ReleaseNormCam()
	ContextActionService:UnbindAction("PlayerInputN")
	ContextActionService:UnbindAction("ZoomNC")
	RunService:UnbindFromRenderStep("CameraUpdateN")
end

return NormalCamera

local ShiftLock = {}

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local player = Players.LocalPlayer

camera.CameraType = Enum.CameraType.Scriptable

local cameraVariables = require(script.Parent.CameraVariables)
local zoomModule = require(script.Parent.Zoom)

local cameraOffset = cameraVariables.cameraOffset
local sensitivity = cameraVariables.sensitivity
local newOffset = cameraVariables.newOffset
local Zoom = zoomModule.Zoom

local function BaseShiftLock(character)
	if not character then
		return
	end
	
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")

	local function playerInput(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then
			cameraVariables.cameraAngleX -= inputObject.Delta.X * sensitivity
			cameraVariables.cameraAngleY = math.clamp(cameraVariables.cameraAngleY - inputObject.Delta.Y * sensitivity, -75, 75)
			UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		end
	end
	
	RunService:BindToRenderStep("CameraUpdateS", Enum.RenderPriority.Camera.Value, function()
		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraVariables.cameraAngleX), 0) * CFrame.Angles(math.rad(cameraVariables.cameraAngleY), 0, 0)
		local cameraCFrame =  startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

		camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
		print(cameraOffset)
	end)
	
	ContextActionService:BindAction("PlayerInputS", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

	ContextActionService:BindAction("ZoomShiftlock", function(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then
			cameraOffset = Zoom(nil, nil, inputObject)
		end
	end, false, Enum.UserInputType.MouseWheel)
end

function ShiftLock.ShiftLock()
	local character = player.Character
	if character then
		BaseShiftLock(character)
	else
		player.CharacterAdded:Connect(function(char)
			BaseShiftLock(char)
		end)
	end
end

function ShiftLock.ReleaseShiftLock()
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	ContextActionService:UnbindAction("PlayerInputS")
	ContextActionService:UnbindAction("ZoomShiftlock")
	RunService:UnbindFromRenderStep("CameraUpdateS")
end

return ShiftLock

1 Like

I’ve had an issue similar to this before. I found that if I changed a value from a module in one script, it didn’t replicate to the others. You need to create a subprogram in the module to update it, then it should replicate.

function CameraVariables:UpdateValue(targetValue, newValue)
    self[targetValue] = newValue
end
1 Like

Umm, I don’t really get it on how I would implement that in my scripts

Whenever you have a line like this, call the function in the CameraVariables module.

--get the amount you want it to be set to
local targetAmount = cameraVariables.cameraAngleX - (inputObject.Delta.X * sensitivity)
--call the procedure in the module to update it
cameraVariables:UpdateValue("cameraAngleX", targetAmount)

and then in the module:

function CameraVariables:UpdateValue(target, newAmount)
    self[target] = newAmount
end
1 Like

Thanks for the help, but I couldn’t implement this solution, but I finally brainstormed a solution. I would make another variable called newShiftlockOffset and newNormalCamOffset that I could transfer between the two scripts and at the start of the script I put that cameraOffset = newShiftlockOffset or newNormalCamOffset and that works.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.