Last active
April 12, 2026 04:43
-
-
Save edutrul/0aa6a1f4fa2f8943a30fc5ccebd962b9 to your computer and use it in GitHub Desktop.
Roblox users joining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local Players = game:GetService("Players") | |
| local ServerStorage = game:GetService("ServerStorage") | |
| local KNIFE_NAME = "Knife" | |
| local HIGHLIGHT_NAME = "KillerHighlight" | |
| local innocentPlayer: Player? | |
| local killerPlayer: Player? | |
| local function applyRole(player: Player) | |
| print("applyRole(player)", player.Name) | |
| local character = player.Character | |
| local backpack = player:FindFirstChildOfClass("Backpack") | |
| if not character or not backpack then | |
| print("NO character neither backpack.") | |
| return | |
| end | |
| print("about to compare Player killer or innocent.", character, character.Name) | |
| print("killerPlayer name", killerPlayer.Name) | |
| print("innocent name", innocentPlayer.Name) | |
| if player ~= killerPlayer then | |
| print(player.Name .. " -> Innocent") | |
| return | |
| end | |
| print(player.Name .. " -> Killer") | |
| -- 🔍 Check existing state BEFORE creating anything | |
| local existingBackpackKnife = backpack:FindFirstChild(KNIFE_NAME) | |
| local existingEquippedKnife = character:FindFirstChild(KNIFE_NAME) | |
| local existingHighlight = character:FindFirstChild(HIGHLIGHT_NAME) | |
| print("existing backpack knife:", existingBackpackKnife) | |
| print("existing equipped knife:", existingEquippedKnife) | |
| print("existing highlight:", existingHighlight) | |
| local highlight = Instance.new("Highlight") | |
| highlight.Name = HIGHLIGHT_NAME | |
| highlight.FillColor = Color3.fromRGB(255, 0, 0) | |
| highlight.OutlineColor = Color3.fromRGB(255, 255, 255) | |
| highlight.Parent = character | |
| local knifeTemplate = ServerStorage:FindFirstChild(KNIFE_NAME) | |
| if not knifeTemplate then | |
| warn("Knife not found in ServerStorage") | |
| return | |
| end | |
| local knifeClone = knifeTemplate:Clone() | |
| knifeClone.Parent = backpack | |
| local humanoid = character:FindFirstChildOfClass("Humanoid") | |
| if humanoid then | |
| humanoid:EquipTool(knifeClone) | |
| end | |
| end | |
| local function assignRoles() | |
| print("assignRoles()") | |
| local players = Players:GetPlayers() | |
| if #players < 2 then | |
| warn("Not enough players to start round") | |
| return | |
| end | |
| innocentPlayer = players[1] | |
| killerPlayer = players[2] | |
| applyRole(innocentPlayer) | |
| applyRole(killerPlayer) | |
| end | |
| local function onPlayerAdded(player: Player) | |
| print("onPlayerAdded(Player)", player.Name) | |
| player.CharacterAppearanceLoaded:Connect(function() | |
| print("CharacterAppearanceLoaded") | |
| applyRole(player) | |
| end) | |
| print("about to call assignRoles() inside from onPlayerAdded(player)") | |
| assignRoles() | |
| end | |
| print("Before Players.PlayerAdded:Connect(onPlayerAdded)") | |
| Players.PlayerAdded:Connect(onPlayerAdded) | |
| print("After the Players.PlayerAdded:Connect(onPlayerAdded)") | |
| -- This loop will not be executed in Roblox studio only in the real game play -- | |
| -- As best practice keep the loop and also the event ...PlayerAdded:Connect() - | |
| for _, player in ipairs(Players:GetPlayers()) do | |
| print("about to call onPlayerAdded(player)", player.Name) | |
| onPlayerAdded(player) | |
| end | |
| print("about to call assignRoles()") | |
| assignRoles() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local Players = game:GetService("Players") | |
| -- Triggers when the character's appearance is loaded. -- | |
| local function onCharacterAppearanceLoaded(character: Model) | |
| print("onCharacterAppearanceLoaded", character.Name) | |
| end | |
| -- Triggers when respawned. -- | |
| local function onCharacterRemoving(character: Model) | |
| print("onCharacterRemoving", character.Name) | |
| end | |
| -- when the character is added to the game. -- | |
| local function onCharacterAdded(character: Model) | |
| print("onCharacterAdded", character.Name) | |
| --[[ | |
| when using :WaitForChild always use :: className | |
| but when using :FindFirstChild use this mechanism below: | |
| local instance = character:FindFirstChild("Humanoid") | |
| if not instance or not instance:IsA("Humanoid") then return end | |
| local humanoid = instance | |
| Because: | |
| FindFirstChild = uncertain | |
| WaitForChild = guaranteed | |
| --]] | |
| local humanoid = character:WaitForChild("Humanoid") :: Humanoid | |
| print("humanoid.Name", humanoid.Name) | |
| print("humanoid.className", humanoid.ClassName) | |
| print("onHumanoidReady", character.Name) | |
| humanoid.Died:Connect(function() | |
| print("--> Humanoid.Died", character.Name) | |
| end) | |
| for _, child in ipairs(character:GetChildren()) do | |
| print(child.Name, "----> className: " .. child.ClassName) | |
| if child:IsA("Tool") then | |
| print("---------> child: ", child.Name, " - className: " .. child.ClassName) | |
| end | |
| end | |
| end | |
| -- happens when a player is connected -- | |
| local function onPlayerAdded(player: Player) | |
| print("onPlayerAdded", player.Name) | |
| player.CharacterAdded:Connect(onCharacterAdded) | |
| player.CharacterRemoving:Connect(onCharacterRemoving) | |
| player.CharacterAppearanceLoaded:Connect(onCharacterAppearanceLoaded) | |
| end | |
| -- Happens when a user leave from the game. -- | |
| local function onPlayerRemoving(player: Player) | |
| print("onPlayerRemoving", player.Name) | |
| end | |
| Players.PlayerAdded:Connect(onPlayerAdded) | |
| Players.PlayerRemoving:Connect(onPlayerRemoving) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local Players = game:GetService("Players") | |
| local function onPlayerAdded(player) | |
| print("[JOIN HANDLER]", player.Name) | |
| end | |
| Players.PlayerAdded:Connect(onPlayerAdded) | |
| task.wait(5) | |
| print("=== EXISTING PLAYERS CHECK ===") | |
| print("Count after wait:", #Players:GetPlayers()) | |
| -- workaround just to delay to demonstrate that existing players work -- NEVER USE IN PROD. -- | |
| task.delay(6, function() | |
| print("Delayed check:", #Players:GetPlayers()) | |
| for _, player in ipairs(Players:GetPlayers()) do | |
| print("[EXISTING AFTER DELAY]", player.Name) | |
| end | |
| end) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local Players = game:GetService("Players") | |
| local onPlayerAdded = function (player) | |
| print('yeah joining a new player') | |
| print(player.Name) | |
| print(player.UserId) | |
| print(player.Neutral) | |
| print(player.TeamColor) | |
| print(player.Parent) | |
| end | |
| Players.PlayerAdded.Connect(Players.PlayerAdded, onPlayerAdded) | |
| -- This code is for the future when users are connected. -- | |
| for _, player in pairs(Players:GetPlayers()) do | |
| print 'Players that exist already in the platform...' | |
| onPlayerAdded(player) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local Players = game:GetService("Players") | |
| local ServerStorage = game:GetService("ServerStorage") | |
| local function onCharacterAdded(player, character) | |
| print("Player:", player.Name, "Character:", character.Name) | |
| local highlight = Instance.new("Highlight") | |
| highlight.FillColor = Color3.fromRGB(255, 0, 0) | |
| highlight.Parent = character | |
| local knife = ServerStorage:WaitForChild("Knife") | |
| local humanoid = character:WaitForChild("Humanoid") | |
| local knifeClone = knife:Clone() | |
| knifeClone.Parent = player.Backpack | |
| humanoid:EquipTool(knifeClone) | |
| end | |
| local function onPlayerAdded(player) | |
| print("Player joined:", player.Name) | |
| player.CharacterAdded:Connect(function(character) | |
| onCharacterAdded(player, character) | |
| end) | |
| if player.Character then | |
| onCharacterAdded(player, player.Character) | |
| end | |
| end | |
| Players.PlayerAdded:Connect(onPlayerAdded) | |
| for _, player in ipairs(Players:GetPlayers()) do | |
| onPlayerAdded(player) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print("Hello world!") | |
| local tool = script.Parent | |
| local handle = tool:WaitForChild("Handle") | |
| local onTouched = function(basePart: BasePart) | |
| print("onTouched", basePart.Name) | |
| end | |
| handle.Touched:Connect(onTouched) | |
| --[[ | |
| When I got closed with a knife this is what happend trigger a lot...: | |
| onTouched RightLowerArm | |
| onTouched RightUpperArm | |
| onTouched Head | |
| onTouched HumanoidRootPart | |
| onTouched Handle | |
| onTouched Baseplate | |
| This is because a character has multiple basePart | |
| Character (Model) | |
| ├── Head (BasePart) | |
| ├── UpperTorso (BasePart) | |
| ├── LowerTorso (BasePart) | |
| ├── LeftArm (BasePart) | |
| ├── RightArm (BasePart) | |
| ├── LeftLeg (BasePart) | |
| ├── RightLeg (BasePart) | |
| ├── HumanoidRootPart (BasePart) | |
| └── Humanoid (NOT a BasePart) | |
| --]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment