Simple Spacecraft Controls

Howdy! I’m way new to scripting, but I’m working on a very simple spaceship and I ran into a couple of problems.

For all intents and purposes, I’m using this tutorial made by Roblox. In this series of tutorials, the camera is top down over the player, but I’d like it to follow them similar to how the default one works.

I don’t need it to be adjustable, I just want it to follow the player and turn as they turn, sort of like having a camera on a pole attached to the player.

I’m pretty sure I need to insert a camera attached to an invisible part, but since the model is meant to replace the default roblox character, it’s in StarterPlayer, so it doesn’t work there.

This is the current code I use, it’s in a localscript that I put in StarterPlayerScripts.

local RunService = game:GetService("RunService")	

local camera = workspace.CurrentCamera
local Run = game:GetService("RunService")
local player = game.Players.LocalPlayer

camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = part

if player.Character then
	local name = player.Character.Name
	part = workspace[name].CameraPart
	local cameraPosition = part.Position
end

local function OnChanged()
	camera.Position = part.Position
	wait()
end

Run.RenderStepped:Connect(OnChanged)

Other than the fact that the whole thing’s a hot mess, what am I doing wrong?

camera.Position --does not exist do this
camera.CFrame = part.CFrame
1 Like

The camera is just stuck in this position

Do you think it’s because the part is currently under StarterPlayer? The model copies over from there to replace the standard player model, is there a way I could bind the camera to the local player? I’ve tried part=player.CameraPart but that’s an invalid member of the local player. From the debug I can see that it’s looking under Players.ManateeDerp47 (me) instead of Workspace.ManateeDerp47, I have a few ideas I want to try though so stand by

Check for any errors in the output

Output says that it’s not a valid member of the player, both when I try with the players.localplayer and workspace.ManateeDerp47

Try putting the script under StarterCharacterScripts.
also, use this instead

if player.Character then
	part = player.Character.CameraPart
end

Try this:

local char = player.Character or player.CharacterAdded:Wait()

if char then
    local name = player.Character.Name
	part = workspace[name].CameraPart
	local cameraPosition = part.Position
end

--// rest of code

The line where part is defined gets the same error, CameraPart is not a valid member of Model “Workspace.ManateeDerp47”

If I keep it in StarterPlayerScripts it’s saying I attempted to index nil with CFrame

If I put it in StarterCharacterScripts it works (yay) but it’s also giving me infinite yields. I’m not too concerned about them, since it still works, but I’m not sure what they mean

Infinite yield means that you are yielding the script for something that doesn’t exist (or just taking extremely long to load)

also why do you need a camera part? I think you can just do this

local charPos = character.HumanoidRootPart.Position
local camPos = charPos + Vector3.new(0, HEIGHT, 0)
camera.CFrame = CFrame.lookAt(camPos, charPos)

I got an output error that HumanoidRootPart isn’t a valid member of Workspace.ManateeDerp47. I fixed most of the infinite yields by moving the control script into StarterCharacterScripts next to the camera script, all that remains is a small alert that ActivateCameraController did not select a module.

As for the “why” of it, that was just the simplest (but not at all efficient) way I could think of to accomplish the end goal

If the part being manipulated is not in workspace, it doesn’t see it as moving since its not a visible object if that makes any sense (this isn’t a technical explanation, it’s just to make sense).

Make sure the part your editing is in workspace. if needed; put a fake part in replicatedstorage and clone it to workspace each time a user enters a spaceship and when they jump out of the spaceship it deletes the part.

havnt seen that one before, it might be new actually. I don’t think its that big of a deal tho, just ignore it

Actually wait you just have to remove camera = scriptable i think

The spaceship is the player, there is no entering and exiting the ship. The part eventually ends up in workspace, it get cloned from StarterPlayer.

The (probably inefficient) solution ended up being as simple as moving the script from StarterPlayerScripts to StarterCharacterScripts, so all I have to do now is hash out the movement.

Yeah, that got rid of it lol, thanks

I would just make the player invisible and weld the spaceship model to the player, to prevent all of this extra stuff but its your choice.