Camera Script Working In Roblox Studio But Doesn't Work In Roblox

Here are the relavent scripts for reference:

SetCamera (LocalScript)

-- This script is responsible for setting the player camera --

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

player.CharacterAdded:Wait()

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = workspace:WaitForChild("CameraBox").CFrame
print("Camera has been set properly.")

TestScript (Server Script)

-- Necessary services --
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")

-- Script Module Folders --
local Modules = ServerScriptService:WaitForChild("Modules")
local PlayerModules = Modules:WaitForChild("PlayerModules")
local CharacterModules = Modules:WaitForChild("CharacterModules")
local WorldModules = Modules:WaitForChild("WorldModules")

-- Player Modules --
local PlayerCameraBox = require(PlayerModules:WaitForChild("PlayerCameraBox"))
local PlayerCameraBoxDestroyer = require(PlayerModules:WaitForChild("PlayerCameraBoxDestroyer"))

-- Character Modules--
local PlayerCharacter = require(CharacterModules:WaitForChild("PlayerCharacter"))

-- World Modules --
local World = require(WorldModules:WaitForChild("World"))
local WorldBuilder = require(WorldModules:WaitForChild("WorldBuilder"))
local WorldDestroyer = require(WorldModules:WaitForChild("WorldDestroyer"))
local WorldDescriptor = require(WorldModules:WaitForChild("WorldDescriptor"))
local WorldPlatformDefiner = require(WorldModules:WaitForChild("WorldPlatformDefiner"))

-- Folders --
local Platforms = ServerStorage:WaitForChild("Platforms")
local CharacterModels = ServerStorage:WaitForChild("CharacterModels")

local world = World.new("Tutorial", Platforms:WaitForChild("BaseGrassPlatform"), 4)
local worldBuilder = WorldBuilder.new(world)

worldBuilder:build(Platforms)

local worldModel = workspace:WaitForChild(world:getWorldName())

WorldPlatformDefiner.setPlatformCharacteristic(world, worldModel)

local worldDescriptor = WorldDescriptor.new(worldModel)
local cameraBox = PlayerCameraBox.new(worldModel, Vector3.new(world:getPlatformModel().PrimaryPart.Position.X, 
										worldDescriptor:describeWorldHeight() / 2,
										-40), Vector3.new(0, 180, 0))

cameraBox:setCameraBox()

Players.CharacterAutoLoads = false

Players.PlayerAdded:Connect(function(player)
	local playerCharacter = PlayerCharacter.new(player, CharacterModels:WaitForChild("DefaultCharacter"))
	playerCharacter:load()
	player.Character:SetPrimaryPartCFrame(workspace:WaitForChild("Tutorial"):WaitForChild("BaseGrassPlatform").Spawn.CFrame)
end)

PlayerCameraBox (Module Script)

local PlayerCameraBox = {}
PlayerCameraBox.__index = PlayerCameraBox

local Modules = script.Parent.Parent
local WorldModules = Modules.WorldModules
local WorldModelManager = require(WorldModules.WorldModelManager)
local WorldDescriptor = require(WorldModules.WorldDescriptor)

function PlayerCameraBox.new(worldModel, position, rotation)
	local playerCameraBox = {}
	playerCameraBox._worldModelManager = WorldModelManager.new(worldModel)
	playerCameraBox._position = position
	playerCameraBox._rotation = rotation
	setmetatable(playerCameraBox, PlayerCameraBox)
	return playerCameraBox
end

-- Sets the camera box relative to the world model --
function PlayerCameraBox:setCameraBox()
	local worldDescriptor = WorldDescriptor.new(self._worldModelManager)
	local worldModel = self._worldModelManager:getWorldModel()
	local cameraBox = Instance.new("Part")
	cameraBox.Name = "CameraBox"
	cameraBox.Size = Vector3.new(2, 2, 2)
	cameraBox.Position = self._position
	cameraBox.Rotation = self._rotation
	cameraBox.Anchored = true
	cameraBox.Transparency = 1
	cameraBox.Parent = workspace
end

return PlayerCameraBox

Anyway, with these scripts, my issue as the title suggests is with the player camera. The player camera works as expected in Roblox studio. Here is a screenshot of how I intend for it to work.

However, when I try publishing the game to roblox to see how it would play out outside of roblox studio, the camera does not set properly, but everything else does. Here is a screenshot of what it looks like in Roblox outside of Roblox Studio.

How do I resolve this issue? I tried removing the camera module from Player Module and while that stopped the camera from focusing on the player from behind, which is also not what I was intending as I want this game to have a 2D perspective, it still doesn’t go where it’s supposed to.

1 Like

Is it printing the “Camera has been set properly.” in output in Roblox?

For some reason, it isn’t. I forgot to mention that as well, for some reason the camera script just isn’t running in roblox but all the other scripts seem to be running as normal.

Only a LocalScript for the Client should control the camera. Also, make sure the Player Character is completely loaded before working with the camera.

That’s what’s happening with my script. The server script is not modifying the player camera, it is just creating a part where the camera is supposed to set its CFrame so it faces the world.

It’s a ServerScript … can’t use that on a single player’s camera.

Look at the first script. The first script is a local script that does modify the players camera. It waits for the character to load as well. The server script doesn’t deal with the players camera at all, it just creates a camera box, which is just an invisible part, for the players camera to relocate itself to.

I found a solution! The solution was simply just doing the following for my local script.

-- This script is responsible for setting the player camera --

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = workspace:WaitForChild("CameraBox").CFrame
print("Camera has been set properly.")
1 Like

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