diff --git a/lib/instances/Game.lua b/lib/instances/Game.lua index 3ebc945..122f71a 100644 --- a/lib/instances/Game.lua +++ b/lib/instances/Game.lua @@ -21,7 +21,7 @@ local Workspace = import("./Workspace") local Game = BaseInstance:extend("DataModel") -function Game:init(instance) +function Game:init(instance, habitat) AnalyticsService:new().Parent = instance ContentProvider:new().Parent = instance CoreGui:new().Parent = instance @@ -40,6 +40,8 @@ function Game:init(instance) UserInputService:new().Parent = instance VirtualInputManager:new().Parent = instance Workspace:new().Parent = instance + + getmetatable(instance).instance.habitat = habitat end function Game.prototype:GetService(serviceName) diff --git a/lib/instances/LocalScript.lua b/lib/instances/LocalScript.lua index 3bcd8b6..f446870 100644 --- a/lib/instances/LocalScript.lua +++ b/lib/instances/LocalScript.lua @@ -1,18 +1,9 @@ --[[ - Serves as just a source container right now. + Currently exactly the same as Script. ]] -local BaseInstance = import("./BaseInstance") -local InstanceProperty = import("../InstanceProperty") +local Script = import("./Script") -local LocalScript = BaseInstance:extend("LocalScript", { - creatable = true, -}) - -LocalScript.properties.Source = InstanceProperty.normal({ - getDefault = function() - return "" - end, -}) +local LocalScript = Script:extend("LocalScript") return LocalScript \ No newline at end of file diff --git a/lib/instances/Script.lua b/lib/instances/Script.lua index 23c8054..97828c1 100644 --- a/lib/instances/Script.lua +++ b/lib/instances/Script.lua @@ -9,6 +9,31 @@ local Script = BaseInstance:extend("Script", { creatable = true, }) +function Script:init(instance) + local internal = getmetatable(instance).instance + + internal.hasRun = false + + instance.AncestryChanged:Connect(function() + if internal.hasRun then + return + end + + local game = instance:FindFirstAncestorOfClass("DataModel") + + if game == nil then + return + end + + internal.hasRun = true + + -- TODO: Correctly handle errors + + local co = coroutine.create(loadstring(instance.Source)) + getmetatable(game).instance.habitat.taskScheduler:schedule(0, co) + end) +end + Script.properties.Source = InstanceProperty.normal({ getDefault = function() return ""