Skip to content

Commit 024df54

Browse files
committed
Local character
1 parent 5c2cdac commit 024df54

File tree

7 files changed

+115
-3
lines changed

7 files changed

+115
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ jobs:
3232
- uses: JohnnyMorganz/stylua-action@v4
3333
with:
3434
token: ${{ secrets.GITHUB_TOKEN }}
35-
version: v2.3.0
35+
version: v2.3.1
3636
args: --check ./lib

docs/Observers/characters.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ Observers.observeCharacter(function(player, character)
2828
end
2929
end)
3030
```
31+
32+
There are scenarios where you only want to observe characters from a select list of players.
33+
This can be done by providing a list of players as the final argument to `observeCharater`:
34+
35+
```lua
36+
Observers.observeCharacter(function(player, character)
37+
...
38+
end, { somePlayer, anotherPlayer }) -- Only observe characters from 'somePlayer' and 'anotherPlayer'
39+
```

docs/Observers/localcharacter.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Local Character
2+
3+
Use the `observeLocalCharacter` observer to observe the lifespan of the local character.
4+
This can only be run on the client.
5+
6+
```lua
7+
Observers.observeLocalCharacter(function(character)
8+
print("Local character has spawned")
9+
return function()
10+
print("Local character has been removed")
11+
end
12+
end)
13+
```

lib/init.luau

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ return {
1212
observeProperty = require(script.observeProperty),
1313
observePlayer = require(script.observePlayer),
1414
observeCharacter = require(script.observeCharacter),
15+
observeLocalCharacter = require(script.observeLocalCharacter),
1516
}

lib/observeCharacter.luau

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,22 @@ type Callback = (player: Player, character: Model) -> (() -> ())?
1919
end
2020
end)
2121
```
22+
23+
An optional `allowedPlayers` array can be provided. Only characters
24+
belonging to players in the array are observed.
25+
26+
```lua
27+
observeCharacter(function(player, character)
28+
-- ...
29+
end, { somePlayer, anotherPlayer })
30+
```
2231
]=]
23-
local function observeCharacter(callback: Callback): () -> ()
32+
local function observeCharacter(callback: Callback, allowedPlayers: { Player }?): () -> ()
2433
return observePlayer(function(player)
34+
if allowedPlayers ~= nil and not table.find(allowedPlayers, player) then
35+
return nil
36+
end
37+
2538
local cleanupFn: (() -> ())? = nil
2639

2740
local characterAddedConn: RBXScriptConnection

lib/observeLocalCharacter.luau

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--!strict
2+
3+
local Players = game:GetService("Players")
4+
local RunService = game:GetService("RunService")
5+
6+
local IS_CLIENT = RunService:IsClient()
7+
8+
type Callback = (character: Model) -> (() -> ())?
9+
10+
--[=[
11+
@within Observers
12+
@client
13+
14+
Creates an observer that captures the local character in the game. This
15+
can only be called from client-side code.
16+
17+
```lua
18+
-- In a LocalScript or Client-context script
19+
observeLocalCharacter(function(character)
20+
print("Local character spawned")
21+
22+
return function()
23+
-- Cleanup
24+
print("Local character removed")
25+
end
26+
end)
27+
```
28+
]=]
29+
local function observeLocalCharacter(callback: Callback): () -> ()
30+
assert(IS_CLIENT, "observeLocalCharacter can only be called from the client")
31+
32+
local cleanup: (() -> ())? = nil
33+
local subscribed = true
34+
35+
local function onCharacterAdded(character: Model)
36+
local cleanupFn: (() -> ())? = nil
37+
38+
local ancestryChangedConn: RBXScriptConnection
39+
ancestryChangedConn = character.AncestryChanged:Connect(function(_, parent)
40+
if parent == nil and ancestryChangedConn.Connected then
41+
ancestryChangedConn:Disconnect()
42+
if typeof(cleanupFn) == "function" and subscribed then
43+
task.spawn(cleanupFn)
44+
if cleanup == cleanupFn then
45+
cleanup = nil
46+
end
47+
end
48+
end
49+
end)
50+
51+
cleanupFn = callback(character)
52+
if typeof(cleanupFn) == "function" then
53+
if ancestryChangedConn.Connected then
54+
cleanup = cleanupFn
55+
else
56+
task.spawn(cleanupFn)
57+
end
58+
end
59+
end
60+
61+
local characterAddedConn = Players.LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
62+
if Players.LocalPlayer.Character ~= nil then
63+
task.spawn(onCharacterAdded, Players.LocalPlayer.Character)
64+
end
65+
66+
return function()
67+
subscribed = false
68+
characterAddedConn:Disconnect()
69+
if typeof(cleanup) == "function" then
70+
task.spawn(cleanup)
71+
cleanup = nil
72+
end
73+
end
74+
end
75+
76+
return observeLocalCharacter

rokit.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# New tools can be added by running `rokit add <tool>` in a terminal.
55

66
[tools]
7-
StyLua = "JohnnyMorganz/StyLua@2.3.0"
7+
StyLua = "JohnnyMorganz/StyLua@2.3.1"
88
selene = "Kampfkarren/selene@0.29.0"
99
wally = "UpliftGames/wally@0.3.2"
1010
rojo = "rojo-rbx/rojo@7.6.0"

0 commit comments

Comments
 (0)