After switching a player’s character to a new character (with a humanoid), I attempt to switch the CameraSubject to that new character’s humanoid using this line of code :
The script is within the character’s model (and it’s not a local script, although that doesn’t make a difference). I’ve tried various other methods, such as this one below : (The idea behind this was to update the CameraSubject upon clicking a part, but that had no effect)
Nothing’s worked at all. My camera type is default but as far as I know that isn’t the problem. Does anyone know a solution?
I not sure that you can change camera from script. Also you can’t fire mouse click from local script. I recommend use Remote Events to do this. You can change camera only from local script in StarterGui.
If you don’t know how to do this. I’ll show you.
First put script to part, which will be clicked and paste this code.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
Next add a Remote Event to ReplicatedStorage.
And the last step is put local script to StarterGui and paste this code.
local newCamera = game.Workspace.Target -- Your camera's subject
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
local camera = game.Workspace.CurrentCamera
camera.CameraSubject = newCamera
end)
Works perfectly and exactly as I needed! Thank you for the help. The only problem I have now is that I need to automate this process as soon as a player clicks the button to switch their character (so no user input is needed to change camera subject).
Would I be able to accomplish this if I just rewrote the first script to detect for a change in the player’s character before executing?
Does i good understand?
So you want to detect when something changes in the player’s character?
Then paste this in script.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(character)
local Childs = character:GetDescendants()
for i, v in pairs(Childs) do
v.Changed:Connect(function()
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
end)
end
end)
end)
But if you want to detect it, for example: when a player puts on a hat. Paste this code.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(character)
character.ChildAdded:Connect(function()
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
end)
end)
end)
And when for example: hat is putted down.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(character)
character.ChildRemoved:Connect(function()
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
end)
end)
end)
But if you’re adding instance to for example: head, then use this code.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(character)
character.DescendantsAdded:Connect(function()
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
end)
end)
end)
But if you’re removing instance to for example: head, then use this code.
local clickdetector = script.Parent.ClickDetector
local event = game.ReplicatedStorage.RemoteEvent -- Your RemoteEvent
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(character)
character.DescendantsRemoved:Connect(function()
clickdetector.MouseClick:Connect(function(player)
event:FireClient(player, game.Workspace.CurrentCamera)
end)
end)
end)
end)
Looks great! I already rewrote the code to detect a player’s character change w/o using a clickdetector but I’ve learned a lot from what you’ve shown me. I’m not a scripter so a lot of this stuff is very new to me, I appreciate the help!