How to change the camera max zoom and min zoom via script?

Hello, i made a pretty basic script so when you not in a car your camera is closer and when you enter the vehicle the camera zooms out (max zoom distance), but for some reason is not working!!! (not a surprise lol)

local player = game.Players.LocalPlayer
local seat = script.Parent

if seat.Occupant == player.Character.Humanoid then
	player.CameraMaxZoomDistance = 27
	player.CameraMinZoomDistance = 28
else
	player.CameraMaxZoomDistance = 10
	player.CameraMinZoomDistance = 11
end
2 Likes

LocalScripts can’t run if they’re parented inside the workspace, what you could probably do instead is use RemoteEvents to check if the Player’s Humanoid is valid to seat.Occupant, and changing the Camera’s ZoomDistances from the client side that way

local Player
local seat = script.Parent
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
        local Character = seat.Occupant.Parent
        Player = game.Players:GetPlayerFromCharacter(Character)

        if Player then
            Event:FireClient(Player, true)
        end
    else
        Event:FireClient(Player, false)
    end
end)
--Client Side, located inside StarterPlayerScripts
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

Event.OnClientEvent:Connect(function(Seated)
    if Seated then
    	player.CameraMaxZoomDistance = 27
    	player.CameraMinZoomDistance = 28
    else
    	player.CameraMaxZoomDistance = 10
    	player.CameraMinZoomDistance = 11
    end
end)

very sorry for the late response but that didn’t work Lol

Welp time to debug then

print("Script running")
local Player
local seat = script.Parent
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("Event fired")
	if seat.Occupant ~= nil then
        local Character = seat.Occupant.Parent
        Player = game.Players:GetPlayerFromCharacter(Character)

        if Player then
            print("Firing Remote to true")
            Event:FireClient(Player, true)
        end
    else
        print("Firing Remote to false")
        Event:FireClient(Player, false)
    end
end)
--Client Side, located inside StarterPlayerScripts
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

Event.OnClientEvent:Connect(function(Seated)
    print("Event received, result:", Seated)
    if Seated then
    	player.CameraMaxZoomDistance = 27
    	player.CameraMinZoomDistance = 28
    else
    	player.CameraMaxZoomDistance = 10
    	player.CameraMinZoomDistance = 11
    end
end)

tried here and its debugging, its printing but what was supposed to happen didn’t, maybe its the way im trying to define the zoom?

What exactly is printing in the Output though? Is the RemoteEvent being firing through right?

yes, its the remote event and basically everything that was supposed to be print

image

i sat on the seat then left

The only thing I see then is that you didn’t either parent this the right way or it’s a Server Script, if you haven’t done so already change it to a LocalScript , and parent it inside StarterPlayerScripts

Why did I reply to myself

heres the image of the hierarchy, i really dont know why roblox overcomplicate such easy things lol

image
camerazoom
image
cameravehicle
image
i changed the name of the remote event on the script too

l o l

That’s really, strange what

Just to make sure everything works correctly…

print("Script running")
local Player
local seat = script.Parent
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("Event fired")
	if seat.Occupant ~= nil then
        local Character = seat.Occupant.Parent
        Player = game.Players:GetPlayerFromCharacter(Character)

        print("Current Player:", Player)
        if Player then
            print("Firing Remote to true")
            Event:FireClient(Player, true)
            print("Test")
        end
    else
        print("Firing Remote to false")
        Event:FireClient(Player, false)
        print("Test")
    end
end)

Try this?

i dont understand this, everything is working fine but not at the same time, it prints but doesnt work

Ok just for experimentation

Could you try this 1 script alone in your seat?

print("Script running")
local Player
local seat = script.Parent
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("Event fired")
	if seat.Occupant ~= nil then
        local Character = seat.Occupant.Parent
        Player = game.Players:GetPlayerFromCharacter(Character)

        print("Current Player:", Player)
        if Player then
        	Player.CameraMaxZoomDistance = 27
        	Player.CameraMinZoomDistance = 28
            --print("Firing Remote to true")
            --Event:FireClient(Player, true)
            print("Test")
        end
    else
        Player.CameraMaxZoomDistance = 10
        Player.CameraMinZoomDistance = 11
       --print("Firing Remote to false")
       --Event:FireClient(Player, false)
        print("Test")
    end
end)

still not working and its saying possible yield at locating the remote event

Read my post here, and try it (I edited it)

1 Like

You destroyed or parented the RemoteEvent somewhere else, keep it but use the script I just gave

1 Like

really thanks for the help, i took your script and @RobloxGamerPro200007 one and made some tweaks and worked thanks for the help everyone!

What the heck there are 2 solutions this is illegal