How to run code when a player joins the game 2

Hello there! So a while ago i asked how to run a line of code when a player joins not when they die, and now i fixed that problem, but now my camera mover script broke because of that. I dont know what im doing wrong and i would help a lot if i could get some awnsers. Heres the script im currently using:
game.Players.PlayerAdded:Connect(function(player)
local tween = game:GetService(“TweenService”):Create(game.Workspace.Camera, TweenInfo.new(-1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, false, 0), {CFrame = (game.Workspace.fancyview.CFrame)})
tween:Play()
end)

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Visible = false
local tween = game:GetService(“TweenService”):Create(game.Workspace.Camera, TweenInfo.new(-1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, false, 0), {CFrame = (game.Workspace.dojocampart.CFrame)})
tween:Play()
end)

Thanks in advance!

1 Like

What has been broken in the camera mover script?

In order for us to don’t lose our eyesight, try to rephrase the script part correctly using three ` in front and at the end of the script

like so

image

game.Players.PlayerAdded:Connect(function(player)
local tween = game:GetService(“TweenService”):Create(game.Workspace.Camera, TweenInfo.new(-1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, false, 0), {CFrame = (game.Workspace.fancyview.CFrame)})
tween:Play()
end)

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Visible = false
local tween = game:GetService(“TweenService”):Create(game.Workspace.Camera, TweenInfo.new(-1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, false, 0), {CFrame = (game.Workspace.dojocampart.CFrame)})
tween:Play()
end)

A PlayerAdded event runs every time an user has joined the game.
A CharacterAdded event runs every time an user’s avatar spawned or respawned.
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

Try keeping the line of code u wanted to run when the player joins using PlayerAdded and create another script using Character Added and insert the camera tweens there.

1 Like

Adding on to what Reinellex said, all you would have to do to fix it is by adding the CharacterAdded event like so:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function() --adding any parameter here would just give you the player's character
		--code here
	end)
end)

Hopefully that fixes your problem!