Need help for camera scripting


As you see in the video, when I click the button my camera attaches to a block and when I click again it does nothing. It was supposed to get back to the player.

Script & hierarchy
local attached = false
local attachButton = script.Parent

attachButton.MouseButton1Click:Connect(function()
	if attached then
		workspace.CurrentCamera.CameraSubject = nil
		attached = false
	else
		local block = script.Component.Value
		if block then
			workspace.CurrentCamera.CameraSubject = block
			attached = true
		end
	end
end)

image (Component is an object value and attached to the “block”)
AND no output errors

The part if attached then is the problem. It only checks if the variable ‘attached’ exists. Try changing it to if attached == false then.

Hope i could help!

You need to put the camerasubject on the humanoid (or player character I’m not sure)

1 Like

No, in the variables, if you just put if varibale then it’s to check if it’s true (the value)

Oh really? I thought it checked if a value existed. Thx!

1 Like

Not sure about that. camerasubject value is nil when it’s attached to a player. but I’ll give it a try

1 Like
local attached = false
local attachButton = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer

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

attachButton.MouseButton1Click:Connect(function()
	if attached then
		workspace.CurrentCamera.CameraSubject = Character
		attached = false
	else
		local block = script.Component.Value
		if block then
			workspace.CurrentCamera.CameraSubject = block
			attached = true
		end
	end
end)

this is the end result, it works. thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.