PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Give Sparkles to Everyone

Give Sparkles to Everyone

Oct 01 2018, 4:07 AM PST

This code sample listens for players spawning and gives them Sparkles in their head. It does this by defining two functions, onPlayerSpawned and onPlayerAdded. It calls these functions for players/characters already in the game, in case the script runs after players already arrive.

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	-- Give them sparkles on their head if they don't have them yet
	if not character:FindFirstChild("Sparkles") then
		local sparkles = Instance.new("Sparkles")
		sparkles.Parent = character:WaitForChild("Head")
	end
end

local function onPlayerAdded(player)
	-- Check if they already spawned in
	if player.Character then
		onCharacterAdded(player.Character)
	end
	-- Listen for the player (re)spawning 
	player.CharacterAdded:Connect(onCharacterAdded)
end

-- Iterate over each player already connected
-- to the game using a generic for-loop
for i, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
-- Listen for newly connected players
Players.PlayerAdded:Connect(onPlayerAdded)