Hello. I have this sort of intro script. It fixes the camera and rotates it, with a GUI and TextButtons.
Anyway, I want this script to only run once, when the player first joins. Currently, it runs when the player joins AND when the player respawns.
It doesn't need to use PlayerAdded. I just thought that'd be the best way to do it. I've already attempted it with no results.
local camera = game.Workspace.CurrentCamera local Players = game:GetService("Players") local plr = Players.LocalPlayer local camPart = game.Workspace.camPart local playClicked = false repeat wait() until plr.Character camPart.Transparency = 1 camera.CameraType = "Scriptable" camera.CFrame = game.Workspace.camPart.CFrame -- I spawn this function so the while loop can execute while the rest of the code does as well. spawn(function() while true do if playClicked then break end camera.CFrame = camera.CFrame * CFrame.Angles(0,0.005,0) wait() end end) -- Takes control from the player and enables the GUI. script.Parent.Enabled = true plr.Character.Humanoid.JumpPower = 0 plr.Character.Humanoid.WalkSpeed = 0 -- We need to de-activate the GUI, restore default camera, break the while loop, and restore player controls. script.Parent.play.MouseButton1Click:Connect(function() script.Parent.Enabled = false camera.CameraType = "Custom" playClicked = true plr.Character.Humanoid.JumpPower = 50 plr.Character.Humanoid.WalkSpeed = 16 end)
before I start, I'm going to make the assumption that this is a localscript in the player and not the player's charcter
The playeradded event fires when a player joins the game, in other words, when the player object is created. It is particularly useful for creating leaderstats, overhead guis, and chat tags. Ex:
game.Players.LocalPlayer.PlayerAdded:Connect(function(plr) local ls = Instance.new("Folder") ls.Name = "leaderstats" local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Value = 2 end)
The player added event is effectively useless in local script because a local script is cloned into the player object from starter gui, starter pack,and starter player scripts. To clone the script into the player object, the player object has to exist first, and the playeradded event fires when a player object is created which is before the local script is cloned into the player.
So something like this will never fire:
game.Players.PlayerAdded:Connect(function(plr) print("hi"..plr.Name) end)
alternatively, if you do this, it will
game.Players.LocalPlayer.CharacterAdded:Connect(function(char) print("hey") end)
(assuming it isnt in the character, if it is, then it doesn't work)
Once you use playerAdded, you can also use CharacterAdded since the game recognizes this event being fired once. Hint, when the character is finally added.
local camera = game.Workspace.CurrentCamera local Players = game:GetService("Players") local plr = Players.LocalPlayer local camPart = game.Workspace.camPart local playClicked = false Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(funtion(Char) camPart.Transparency = 1 camera.CameraType = "Scriptable" camera.CFrame = game.Workspace.camPart.CFrame script.Parent.Enabled = true plr.Character.Humanoid.JumpPower = 0 plr.Character.Humanoid.WalkSpeed = 0 while true do if playClicked then break end camera.CFrame = camera.CFrame * CFrame.Angles(0,0.005,0) wait() end end) end)