Specific player camera not changing in-game?

Hello Devs,

I’ve created a script that, when you press a button you become a sort of Part/Model. Whenever the button is clicked, your character goes invisible and is unable to move. The part/model is cloned into the workspace in the position of the player. The camera then switches to the part. (Keep in mind this is supposed to be player specific, which means it’s only supposed to happen to one player)

The script works In studio. But whenever I test the script in-game, the camera no longer switches to the part. But everything else works fine.

I have two scripts, one inside serverscriptservice, and the other inside starterGUI.

Here is my current code: (Local script inside starterGUI)

local Camera = workspace.CurrentCamera
local remote = game.ReplicatedStorage:WaitForChild("Remote")
local value = game.workspace.Values.Walllnut 

function onClicked(Player)
    print("ButtonHasBeenPressed")
    remote:FireServer("ToasterIsFired")
    local number = value.Value
    print(number)
    local part = workspace:WaitForChild(number)
    wait(0.1)
    Camera.CameraType = Enum.CameraType.Custom
    Camera.CameraSubject = part:WaitForChild("Handle")
end
game.workspace.StartButton.ClickDetector.MouseClick:connect(onClicked)

(Script inside serverscriptservice)

local object = game.workspace.Wallnut
local remote = game.ReplicatedStorage:WaitForChild("Remote")
local value = game.workspace.Values.Walllnut

remote.OnServerEvent:Connect(function(player, request)
    if request == "ToasterIsFired" then
        local number = math.random(1,999999)
        value.Value = number
        print(number)
        print("Request Received")
        local ClonedObject = object:Clone()
        ClonedObject.Parent = workspace
        ClonedObject.Name = (number)
        ClonedObject.Handle.Position = player.Character.Torso.Position
        player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.TeleportPart.Position)
    end
end)

If you need more info about the script, let me know and I can explain in a bit more detail.

If anyone could please help me or point me in the right direction, that would be greatly appreciated! (Sorry if the script is very confusing to read)

It might be a problem with your anti-exploit script or something. I quickly made a similar script and it worked for me in studio Test mode. You could look at my code to see anything that you could do differently.

I think it could be because of that math.random(1,999999) part and the player may not be getting the correct number or something. Try printing on the localplayer script.

-- Player
local camera = workspace.CurrentCamera
local remote = game:GetService("ReplicatedStorage").Remotes.hey

game:GetService("ContextActionService"):BindAction("LookAtPart", function(actionName, inputState, inputObject)
    if inputState == Enum.UserInputState.Begin then
    	remote:FireServer()
	    local part = workspace:WaitForChild(game.Players.LocalPlayer.Name .. "ye")
    	camera.CameraType = Enum.CameraType.Custom
    	camera.CameraSubject = part
    end
end, false, Enum.KeyCode.E)
-- Server
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("hey")

remote.OnServerEvent:Connect(function(plr)
	local part = Instance.new"Part"
	part.Name = plr.Name .. 'ye'
	part.Parent = workspace
end)

I appreciate the help! But I was only wondering why my script only works in studio but not in Roblox itself. Thanks anyway for the help! (also it wasn’t the math.random, I changed it and the same thing happened. The only reason I put that there was so that the player camera wouldn’t switch to an already existing part/model)

The best reason I can think of that it would work on studio but not in-game is because of latency. There is a lot less latency when testing on Studio since it starts with your system but in-game, there will be lots of differences in performance, such as with wait(), RunService.Heartbeat:Wait(), and :FireServer() functions. Things like that. Like, it could be possible that your :WaitForChild() even timed out because it was trying to wait for something for too long.

Alright, I’ll have a look into it. If I find anything I’ll post it here.

Also would team create have anything to do with it? I’m currently in a team create with some of my friends. I have been commiting the scripts before publishing.

In the dev console it comes up with this error, I don’t understand what it means…
image

It might have to deal with Team Create, I don’t know. I don’t understand that error either. Maybe the script changes are applying awkwardly so it doesn’t get published properly? You can try playing around with it.