What do you want to achieve? I am attempting to make the player’s camera a certain part in the workspace.
What is the issue? When I try to do this through a local script, the local script returns a wait for child error, or says that the part does not exist.
What solutions have you tried so far? I’ve looked around for answers but most didn’t seem well enough to help me. I’ve also tried making the part singular (rather than in a model) but still no luck.
I would make a remote function, going from client to server and then back to the client, but this seems extremely unnecessary as I’ve done this before.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("PlayerJoined")
-- Ignore the passed player; start with additional passed data
local function onfreezeplayer(picked_value)
print("Picked value:")
print(picked_value)
print("Incoming server event...")
local lplayer = game.Players.LocalPlayer
print(lplayer)
repeat wait() until lplayer.Character
local char = lplayer.Character
local head = char:WaitForChild('Head')
char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(picked_value))
print("e")
head.Anchored = true
print("Your head should be anchored")
local humanoid = char.Humanoid
humanoid.WalkSpeed = 0
--change cam
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = game.Workspace.SpawnBoxes.SB1cam
-- Fire the remote event
remoteEvent:FireServer()
end
-- Call "onNotifyPlayer()" when the server fires the remote event
remoteEvent.OnClientEvent:Connect(onfreezeplayer)
Ok so I’ve updated my script and included a wait for child and I still get an error saying there’s an infinite yield. I can see the part in the workspace (Literally this time) but the game thinks it’s not there?
Yes, this is most likely the issue. The server will always have access to everything in workspace, but for the client it depends whether or not the part is streamed in. As mentioned in api you should use waitForChild (which you did) but this doesn’t mean the player will ever go near that part and/or actually stream it in.
What I would do is store your “Camera parts” in a folder inside ReplicatedStoarge. Then reference that part in replicatedStorage instead of referencing it in workspace. You will also have to use :RequestStreamAroundAsync() in order for the map to load in that area. Otherwise the camera will cframe but there will be nothing pretty to look at.
Note that I’m typing on mobile so there may be typos.
Thank you for the help! I was able to try this and the camera now goes to the correct position, but the player does not teleport. I’m not sure if this is related to the fix, but before the player would teleport.
Here’s the entire script
local remoteEvent = ReplicatedStorage:WaitForChild("PlayerJoined")
-- Ignore the passed player; start with additional passed data
local function onfreezeplayer(picked_value)
local lplayer = game.Players.LocalPlayer
print(lplayer)
local spawnbox = CFrame.new(179.5, 16108.009, -3258.5)
lplayer:RequestStreamAroundAsync(spawnbox.Position)
print("Picked value:")
print(picked_value)
print("Incoming server event...")
--change cam
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local WFC = game.ReplicatedStorage:WaitForChild("SB1cam")
camera.CFrame = game.ReplicatedStorage.SB1cam.CFrame
--Anchor head and stop player from moving
repeat wait() until lplayer.Character
local char = lplayer.Character
local head = char:WaitForChild('Head')
char.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(picked_value))
print(picked_value)
head.Anchored = true
print("Your head should be anchored")
local humanoid = char.Humanoid
humanoid.WalkSpeed = 0
end
-- Call "onNotifyPlayer()" when the server fires the remote event
remoteEvent.OnClientEvent:Connect(onfreezeplayer)
The player just spawns at what i presume is the default spawning location.