I feel like an idiot; I’ve been working to solve this bug for hours now and everything I try leads to nothing.
I have a StarterCharacterScript that requires a ModuleScript that updates the camera for the local character by lerping a camera part and updating CurrentCamera to match it. The position of the camera without offsets updates properly and is unique for every client.
This script also handles some other variables that displace/bound the camera, which are updated by module functions called by the StarterCharacterScript.
However, when a player causes one of these variables to update, this variable is updated on all clients.
Am I misunderstanding something here? From what I understand, requiring a ModuleScript within a LocalScript (such as a StarterCharacterScript) executes the script locally. So why would the script affect other clients?
I am at a lost on how to fix this issue. Any help is appreciated!
You’re right in your understanding that a ModuleScript required locally maintains a state local to each client (including if the server is the environment requiring a ModuleScript).
Something else must be going on here, but I’m not seeing anything obvious from what you’ve mentioned so far… What is an example of a when “a player causes one of these variables to update”? Could you share some relevant portions of the ModuleScript code?
Let me preface with the fact that this is for a 2D platformer. Here’s a clip of what it is supposed to look like:
I have parts that act as zones that have number attributes for various things related to camera behavior (i.e. YOffset => how much to vertically offset the camera’s position).
When the player’s hitbox (a rectangular part) touches the part, the StarterCharacterScript sends the zone part to the camera ModuleScript, where the attribute values are accessed.
The CameraPart used by the ModuleScript has NumberValues, one for each zone attribute. The values of these NumberValues are then tweened to the new value given by the zone attributes.
-- updating values in the StarterCharacterScript
local cameraZoneContainer = workspace.Level.CameraZones:GetChildren()
for _, zone in pairs(cameraZoneContainer) do
zone.Touched:Connect(function(hit)
if hit.Name == "PlayerHitbox" then
cameraModule.updateCamera(zone)
end
end)
end
-- example with YOffset
local camera = workspace.CurrentCamera
local cameraPart = workspace.CameraPart
local YOffset = cameraPart:WaitForChild("YOffset")
local yOffsetTween
function module.updateCamera(zone)
-- I omitted other variables like bounds
local targetYOffset = zone:GetAttribute("YOffset")
if yOffsetTween then yOffsetTween:Cancel() end
yOffsetTween = TweenService:Create(YOffset, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), { Value = targetYOffset })
yOffsetTween:Play()
end
function module.cameraOnUpdate(dt)
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
-- I omitted lerping and smoothing stuff here
-- limit camera to bounds and update camera to match
camera.CFrame = CFrame.new(math.clamp(cameraPart.CFrame.X, leftBound.Value, rightBound.Value),
math.clamp(cameraPart.CFrame.Y, bottomBound.Value, topBound.Value) + YOffset.Value,
35 + Zoom.Value)
end
end
I thought that maybe NumberValues were to blame, for some reason. I changed the code to just ease numerical values stored by variables, but the issue persists.
Sorry if the code is bad. I’ve only started learning Lua around November.
I tunnel visioned on the ModuleScript thinking it had something to do with that, completely ignoring the terrible condition I set in that if statement…