📅  最后修改于: 2023-12-03 15:04:54.889000             🧑  作者: Mango
Roblox PlayerAdded is a built-in event in the Roblox platform. It triggers whenever a player joins the game.
game.Players.PlayerAdded:Connect(function(player)
-- code to execute when a player joins the game
end)
player
- The player that joined the game.The PlayerAdded event is commonly used to set up player-specific data, such as setting up a player's GUI, setting their initial stats, and more.
game.Players.PlayerAdded:Connect(function(player)
-- create player GUI
local playerGui = Instance.new("ScreenGui")
playerGui.Parent = player.PlayerGui
-- setup player stats
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.MaxHealth = 100
humanoid.Health = 100
end)
end)
In the above code, we create a new ScreenGui instance whenever a new player joins the game, and we also ensure that a joining player has a maximum health of 100 and full health upon joining.
game.Players.PlayerAdded:Connect(function(player)
-- create a new TextBox for each player's messages
local chatBox = Instance.new("TextBox")
chatBox.Position = UDim2.new(0.1, 0, 0.85, 0)
chatBox.Size = UDim2.new(0.8, 0, 0.1, 0)
chatBox.BackgroundTransparency = 0.5
chatBox.TextColor3 = Color3.new(1, 1, 1)
chatBox.TextScaled = true
chatBox.Parent = player.PlayerGui
-- connect each player's chatBox to a central messaging system
chatBox.FocusLost:Connect(function()
local message = chatBox.Text
chatBox.Text = ""
-- send the message to other players
for _, otherPlayer in pairs(game.Players:GetPlayers()) do
if otherPlayer ~= player then
local otherChatBox = otherPlayer.PlayerGui:FindFirstChild("ChatBox")
if otherChatBox then
otherChatBox.Text = player.Name .. ": " .. message
end
end
end
end)
end)
In the example above, we create a new TextBox for each player's chat messages and connect each box to a central messaging system that sends messages to all other players in the game.
-- initialize leaderboard values
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = game:GetService("ServerStorage")
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
-- connect to PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
-- clone leaderboard for each player
local clone = leaderstats:Clone()
clone.Parent = player
-- increment score upon victory
player.CharacterAdded:Connect(function(character)
local tag = character:FindFirstChild("Head"):FindFirstChild("Tag")
if not tag then
tag = Instance.new("BoolValue")
tag.Name = "Tag"
tag.Parent = character:FindFirstChild("Head")
end
tag.Changed:Connect(function(newValue)
if newValue == true then
local wins = player.leaderstats.Wins
wins.Value = wins.Value + 1
end
end)
end)
end)
In the example above, we initialize a leaderboard with a wins counter and add one to the counter for each victory a player earns within the game. We also ensure that each player has their own leaderboard instance by cloning the initial instance for each joining player.