ModuleScript (모듈 스크립트)
Empty
Empty
Empty
ModuleScript는 서버 스크립트와 클라이언트 스크립트에서 모두 사용할 수 있는 재사용 가능한 코드 조각입니다.
Empty
Empty
Empty
다양한 곳에서 사용되는 코드를 조직화하는 데 도움이 되며, 게임의 다른 스크립트 간에 함수와 데이터를 쉽게 공유할 수 있게 합니다.
Empty
Empty
Empty
유틸리티 라이브러리 (Utility Libraries) | 수학 연산 또는 데이터 변환과 같은 일반적인 작업 |
게임 상태 관리 (Game State Management) | 플레이어 점수, 레벨 또는 게임 단계를 추적하는 등 게임의 전체 상태를 관리합니다. |
데이터 관리 (Data Management) | 여러 스크립트에서 접근하고 수정해야 하는 플레이어 데이터를 저장하고 관리합니다. |
공유 설정 (Shared Configurations) | 다른 스크립트에서 공유해야 하는 설정 또는 상수를 저장합니다. |
Empty
Empty
Empty
local UtilityModule = {}
function UtilityModule.reverseString(str)
return str:reverse()
end
function UtilityModule.roundNumber(num)
return math.floor(num + 0.5)
end
return UtilityModule
Empty
Empty
Empty
local GameStateModule = {}
GameStateModule.scores = {}
function GameStateModule.addScore(player, score)
if not GameStateModule.scores[player] then
GameStateModule.scores[player] = 0
end
GameStateModule.scores[player] = GameStateModule.scores[player] + score
end
function GameStateModule.getScore(player)
return GameStateModule.scores[player] or 0
end
return GameStateModule
Empty
Empty
Empty
local PlayerDataModule = {}
PlayerDataModule.data = {}
function PlayerDataModule.setData(player, key, value)
if not PlayerDataModule.data[player.UserId] then
PlayerDataModule.data[player.UserId] = {}
end
PlayerDataModule.data[player.UserId][key] = value
end
function PlayerDataModule.getData(player, key)
return PlayerDataModule.data[player.UserId] and PlayerDataModule.data[player.UserId][key]
End
return PlayerDataModule
Empty
Empty
Empty
local ConfigModule = {}
ConfigModule.GRAVITY = 196.2
ConfigModule.MAX_PLAYERS = 10
return ConfigModule
Empty
Empty
Empty
local Utilities = require(game.ReplicatedStorage.UtilityModule)
print(Utilities.reverseString("Roblox"))
print(Utilities.roundNumber(3.6))
local GameState = require(game.ServerScriptService.GameStateModule)
game.Players.PlayerAdded:Connect(function(player)
GameState.addScore(player, 10)
print("Score for player " .. player.Name .. ": " .. GameState.getScore(player))
end)