Help me fix camera bug

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I would like to have a working character-choosing system as a randomizer

  1. What is the issue?

The issue is that when I reset my character or I die in-game, the camera breaks

Here’s a video:
robloxapp-20240119-1326515.wmv (1.7 MB)

  1. What solutions have you tried so far?

I’ve used AI to try to fix it, but it failed.

This is my script:

local Chances = require(script.Chances)

game.Players.PlayerAdded:Connect(function(plr)
	local characterAddedConn
	local characterRemovingConn

	characterAddedConn = plr.CharacterAdded:Connect(function(char)
		local Chosen = Chances.PickObject()
		local Clone = game.ServerStorage.Characters:WaitForChild(Chosen.Name):Clone()
		Clone.Name = plr.Name
		local Rootp = Clone:FindFirstChild("HumanoidRootPart")
		local Plrp = char:FindFirstChild("HumanoidRootPart")
		if Rootp and Plrp then
			Rootp.CFrame = Plrp.CFrame
		end
		char:Destroy()
		Clone.Parent = workspace
		for i, part in pairs(Clone:GetDescendants()) do
			if part:IsA("BasePart") then
				part.Anchored = false
			end
		end
		plr.Character = Clone

		-- Set HumanoidDescription for the chosen character
		local humanoid = Clone:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local humanoidDescription = Instance.new("HumanoidDescription",Clone)
			humanoidDescription.HatAccessory = Chosen.HatAccessory
			humanoidDescription.BodyTypeScale = Chosen.BodyTypeScale
			humanoidDescription.ClimbAnimation = Chosen.ClimbAnimation
			humanoidDescription.Face = Chosen.Face
			humanoidDescription.GraphicTShirt = Chosen.GraphicTShirt
			humanoidDescription.HeadColor = Chosen.HeadColor           
			humanoid:ApplyDescription(humanoidDescription)
		end

		-- Update camera position
		local camera = workspace.CurrentCamera
		
		while wait() do
			camera.CameraSubject = Clone:FindFirstChildOfClass("Humanoid")
		end
		-- Disconnect the characterAddedConn and characterRemovingConn when the character is removed
		characterRemovingConn = plr.CharacterRemoving:Connect(function()
			characterAddedConn:Disconnect()
			characterRemovingConn:Disconnect()
		end)
	end)
end)

I may be off here. But, I see you’re working with the player once loaded then getting the character from that. When the player dies or resets that isn’t going to fire as they are already in the game. But this will fire on each reload while still in game …

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        --  code 
    end)
end)
1 Like

I was about to say that and you are early than me

1 Like

the changes I made doesn’t seem like to work…

I would guess it’s just a matter of setting that up (the cam) on each reload …
You may have to modify your code to do that.

exactly, I was thinking that when the player respawns, it would use the CharacterAdded:Connect(function(char)) right?

It seems like you have an error with CameraSubject (or maybe not)

Yes … looks like that is really the only way you have to do this.

1 Like

Maybe it is, I am not a good scripter (But normal)…

2 Likes