Why is my camera not changing?

My script is supposed to change the player’s CameraMode and CameraMaxZoomDistance locally when they equip the tool. The local script is a child of the tool.

image

1 Like

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local StarterPack = game:GetService("StarterPack")

local CameraMode = StarterPlayer.CameraMode
local CameraMaxZoomDistance = StarterPlayer.CameraMaxZoomDistance
local DefaultCameraMode = Enum.CameraMode.Classic
local DefaultMaxZoomDistance = 128

script.Parent.Equipped:Connect(function()
	if CameraMode == Enum.CameraMode.Classic then
		CameraMode = Enum.CameraMode.LockFirstPerson
		CameraMaxZoomDistance = 0.5
	end
end)

script.Parent.Unequipped:Connect(function()
	if CameraMode == Enum.CameraMode.LockFirstPerson then
		CameraMode = DefaultCameraMode
		CameraMaxZoomDistance = DefaultMaxZoomDistance
	end
end)

Doesn’t make any changes in-game.

local StarterPlayer = game:GetService("StarterPlayer")

local CameraMode = StarterPlayer.CameraMode
local CameraMaxZoomDistance = StarterPlayer.CameraMaxZoomDistance

You’ve got StarterPlayer assigned to the Service and and not an actual player. The CameraMode and CameraMaxZoomDistance properties on StarterPlayer don’t retroactively affect players that have already joined the game, they only dictate the default. You should be setting a Player's CameraMode and CameraMaxZoomDistance instead. To get the actual Player, you can just use:

local Player = game:GetService("Players").LocalPlayer
1 Like

You should use Players | Roblox Creator Documentation instead of StarterPlayer | Roblox Creator Documentation

Also, why do you have a variable with servertorage if you can’t access the servertorage in a local script?

Also in CameraMode you are only changing that in the variable, not in the player camera.

Here is the fixed script:

local player = game.Players.LocalPlayer

script.Parent.Equipped:Connect(function()
	if player.CameraMode == Enum.CameraMode.Classic then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		player.CameraMaxZoomDistance = 0.5
	end
end)

script.Parent.Unequipped:Connect(function()
	if player.CameraMode == Enum.CameraMode.LockFirstPerson then
		player.CameraMode = Enum.CameraMode.Classic
		player.CameraMaxZoomDistance = 128
	end
end)
3 Likes