LocalScript works on studio but not when I play the game

local player = game.Players.LocalPlayer
-- Wait for the character to load
player.CharacterAdded:Wait()
print("added")

local car = workspace:WaitForChild("StarterCharacter")
local cam = workspace.CurrentCamera -- Get the current camera

-- Set camera properties
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = car-- Set the camera to follow the car

print("Camera set up completed")

for some reason the script only works on the studio, I edited it to be sure that the character is added first but still nothing changed.

4 Likes

you mean even line 4 doesn’t print, or just the end of the script doesn’t work?

2 Likes

even the prints does not works, I tried even to put a print statement in each line and nothing printed. I just get a red line of the script location in the StarterCharacterScripts and in the character.

1 Like

try

local player = game.Players.LocalPlayer
-- Wait for the character to load
workspace:WaitForChild(player.Name)

if this doesn’t work then idk

2 Likes

Does not work, idk if its something in the game settings. The game is private tho but I don’t think it matters.

1 Like

you dont have to wait for character added. when you put the script in StarterCharacterScript it alway be cloned and parented to character. You should remove that line
(Also if that car is model i suggest you use car.PrimaryPart or something like body of the car)

3 Likes

I thought that would work but not, this is the output i get in the game btw.

the edited script

local player = game.Players.LocalPlayer

local car = workspace:WaitForChild("StarterCharacter")
local cam = workspace.CurrentCamera -- Get the current camera

-- Set camera properties
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = car.PrimaryPart -- Set the camera to follow the car

print("Camera set up completed")

1 Like

For some reason, the Player is sometimes added before this script is instantiated. So in order to factor this, you have to also check if the player exists in first place and then wait if it isn’t there yet.

2 Likes

not working either>>>>>>>sssssssssssss

1 Like

have you tried printing something in the first line of the script?

2 Likes

screw it try to put wait() on the very first line (before player) it worked for me

2 Likes

yea, it did not print either…[

1 Like

if you have this script in starter character scripts then it won’t pass the line where it’s waiting for the character to be added since the event your waiting is already happened try to remove any yields

2 Likes

are you sure its a localscript and not a regular server script?

2 Likes

yea i told him to remove already

3 Likes

he can only define camera on local script so it is local script

2 Likes

still nothing works, its so weird that it perfectly works fine in the studio. yea its a local script

1 Like

weird it worked me both on studio and on the game

1 Like

have you set the model PrimaryPart? like in the Properties

1 Like

yes i did, when i run the script on the studio it works fine, but not in the game