Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created April 12, 2026 04:52
Show Gist options
  • Select an option

  • Save edutrul/5b314a88ef0b7c7e4c02654f565adc44 to your computer and use it in GitHub Desktop.

Select an option

Save edutrul/5b314a88ef0b7c7e4c02654f565adc44 to your computer and use it in GitHub Desktop.
Advanced lifecycle
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local KNIFE_NAME = "Knife"
local HIGHLIGHT_NAME = "KillerHighlight"
-- 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
local player = Players:GetPlayerFromCharacter(character)
if not player then
print("No player found for character")
return
end
local backpack = player:FindFirstChildOfClass("Backpack")
if not backpack then
print("No backpack found")
return
end
local existingBackpackKnife = backpack:FindFirstChild(KNIFE_NAME)
local existingEquippedKnife = character:FindFirstChild(KNIFE_NAME)
print("existing backpack knife:", existingBackpackKnife)
print("existing equipped knife:", existingEquippedKnife)
if existingBackpackKnife or existingEquippedKnife then
print("Knife already exists, skipping clone")
return
end
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
-- 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)
print("Hello world!")
local Players = game:GetService("Players")
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local onTouched = function(basePart: BasePart)
print("onTouched", basePart.Name)
local character = basePart:FindFirstAncestorOfClass("Model")
if not character then return end
print('className', character.ClassName)
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
local targetPlayer = Players:GetPlayerFromCharacter(character)
if not targetPlayer then return end
local attackerCharacter = tool:FindFirstAncestorOfClass("Model")
if not attackerCharacter then return end
local attackerPlayer = Players:GetPlayerFromCharacter(attackerCharacter)
if not attackerPlayer then return end
print("attacker:", attackerPlayer.Name, "target:", targetPlayer.Name)
if targetPlayer == attackerPlayer then return end
humanoid.Health = 0
end
handle.Touched:Connect(onTouched)
local tool = script.Parent
tool.Equipped:Connect(function()
print("Tool Equipped:", tool.Name)
end)
tool.Unequipped:Connect(function()
print("Tool Unequipped:", tool.Name)
end)
tool.Activated:Connect(function()
print("Tool Activated (click):", tool.Name)
end)
tool.Deactivated:Connect(function()
print("Tool Deactivated (release):", tool.Name)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment