goopler/src/ServerScriptService/ecs/systems/server/playersArePlayerCharacters.ts

35 lines
No EOL
1.2 KiB
TypeScript

import { Players } from "@rbxts/services"
import { World, useEvent } from "@rbxts/matter"
import { Health, Model, PlayerCharacter } from "ReplicatedStorage/ecs/components"
import { CharacterRigR6 } from "@rbxts/character-promise"
/**
* Player characters are marked with the "PlayerCharacter" component, "Health" component, and "Model" component.
*/
function playersArePlayerCharacters(world: World): void {
Players.GetPlayers().forEach((player, _) => {
for (const [_, character] of useEvent(player, "CharacterAdded")) {
world.spawn(
Model({
model: character
}),
PlayerCharacter({
character: character as CharacterRigR6,
player: Players.GetPlayerFromCharacter(character) as Player,
humanoid: character.WaitForChild("Humanoid") as Humanoid
}),
Health({
health: 80,
maxHealth: 100,
regeneration: 0.25
})
)
}
})
for (const [id] of world.query(PlayerCharacter).without(Model)) {
world.despawn(id)
}
}
export = playersArePlayerCharacters