Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use PlayerAdded event?

Asked by
Hypgnosis 186
5 years ago
Edited 5 years ago

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)
0
Where is this script located? SPS, StarterGui etc? SummerEquinox 643 — 5y
0
It's a LocalScript inside of a ScreenGUI. Hypgnosis 186 — 5y
0
Okay so, if you only want it to run once, you can either handle this from StarterPlayerScripts, or you can disable the ResetOnSpawn property of your ScreenGui provided it doesn't break it. I always handle my UI from StarterPlayerScripts. SummerEquinox 643 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

before I start, I'm going to make the assumption that this is a localscript in the player and not the player's charcter

Basic explanation


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)

Local scripts


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)

0
So is there any way to make my script work only when the player joins, and not when they respawn? Hypgnosis 186 — 5y
0
Because CharacterAdded fires every time they respawn. Hypgnosis 186 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
PlayerAdded doesn't catch LocalPlayer's add. SummerEquinox 643 — 5y

Answer this question