Hello, so I’ve been trying to make a script that detects if a player gets added to workspace, but i cant figure out how. This is my current script that detects if a player gets added to a folder.
local Characters = workspace:WaitForChild("Characters")
Characters.ChildAdded:Connect(v4)
for i, v in pairs(Characters:GetChildren()) do
v4(v)
end
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
--This built-in function will detect every time the Character is added from the Player.
end)
end)
local Workspace = game:GetService("Workspace")
local function CreateCharactersFolder()
local Folder = Instance.new("Folder")
Folder.Name = "Characters"
Folder.Parent = Workspace
return Folder
end
-- Failsafe function for cases where Workspace.Characters wasn't created / indexes nil
local Characters = Workspace:FindFirstChild("Characters") or CreateCharactersFolder()
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
repeat task.wait(0) until Characters ~= nil
-- Ensure Workspace.Characters exists
if Characters then
-- Double check for the existence of Workspace.Characters
Character.Parent = Characters
-- Parent the new Character to Workspace.Characters
end
end)
end)
workspace.ChildAdded:Connect(function(child) --Detects when a new Instance is added to the workspace
if not child:IsA("Model") then return end --stop the code if the instance is not a model
for i,player in pairs(game.Players:GetPlayers()) do --loop through all the players and see if their character is the instance added to the workspace
if player.Character == child then
--the instance added is their character
local Character = child
end
end
end)