How would I go about loading something from a github repo into my game?

Hey devforum! I’m wondering how I would go about loading something from a github repo into a game.

I’m working on a framework and want to allow the user to use a CLI Plugin to install a package (which is a ModuleScript) located inside of a github repo and then parent that to a folder inside of the game.

I’m familiar with the github REST Api but not too familiar with HttpsService which I’m assuming would be required to kickstart this project. I’m also familiar with how an Instance’s JSON format would look like and I’m pretty good working with that.

I just want to know two things:

  1. Is it possible to accomplish this?
  2. The basic steps required to accomplish this.

Any help much appreicated.
Thanks devforum!

2 Likes

It is possible, I have done it to inject shared dependencies across games.

Code example:


local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- GitHub repository details
local owner = "username"
local repo = "repository"
local path = "path/to/ModuleScript.lua"

-- Function to fetch raw content from GitHub
local function fetchModuleScriptFromGitHub()
    local url = string.format("https://api.github.com/repos/%s/%s/contents/%s", owner, repo, path)
    local response = HttpService:RequestAsync({
        Url = url,
        Method = "GET"
    })

    if response.Success then
        local data = HttpService:JSONDecode(response.Body)
        if data.content then
            -- Decode the base64 content
            local decodedContent = HttpService:Base64Decode(data.content)
            return decodedContent
        else
            error("Content not found in the response")
        end
    else
        error("Failed to fetch content from GitHub: " .. response.StatusCode)
    end
end

-- Function to create and parent the ModuleScript
local function createModuleScript(name, content)
    local moduleScript = Instance.new("ModuleScript")
    moduleScript.Name = name
    moduleScript.Source = content
    moduleScript.Parent = ReplicatedStorage -- Change this to the desired folder
    return moduleScript
end

-- Main function to install the package
local function installPackage()
    local content = fetchModuleScriptFromGitHub()
    if content then
        createModuleScript("MyModuleScript", content)
        print("ModuleScript installed successfully!")
    else
        error("Failed to fetch ModuleScript content")
    end
end

-- Run the installation
installPackage()

2 Likes

Tried a quick test and it did not work. Here’s the plugin code:

local Toolbar = plugin:CreateToolbar("My plugin")
local InsertButton = Toolbar:CreateButton("Do a test", "", "rbxassetid://4998267428")
local Selection = game:GetService("Selection")

InsertButton.ClickableWhenViewportHidden = true
InsertButton.Click:Connect(function()
	local HttpService = game:GetService("HttpService")
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	
	-- GitHub repository details
	local owner = "veternitzz"
	local repo = "dotrbx-packages"
	local path = "ModuleScript.lua"

	-- Function to fetch raw content from GitHub
	local function fetchModuleScriptFromGitHub()
		local url = string.format("https://api.github.com/repos/%s/%s/contents/%s", owner, repo, path)
		local response = HttpService:RequestAsync({
			Url = url,
			Method = "GET"
		})

		if response.Success then
			local data = HttpService:JSONDecode(response.Body)
			if data.content then
				-- Decode the base64 content
				local decodedContent = HttpService:Base64Decode(data.content)
				return decodedContent
			else
				warn("Content not found in the response")
				return
			end
		else
			warn("Failed to fetch content from GitHub: " .. response.StatusCode)
			return
		end
	end

	-- Function to create and parent the ModuleScript
	local function createModuleScript(name, content)
		local moduleScript = Instance.new("ModuleScript")
		moduleScript.Name = name
		moduleScript.Source = content
		moduleScript.Parent = ReplicatedStorage -- Change this to the desired folder
		return moduleScript
	end

	-- Main function to install the package
	local function installPackage()
		local content = fetchModuleScriptFromGitHub()
		if content then
			createModuleScript("MyModuleScript", content)
			print("ModuleScript installed successfully!")
		else
			error("Failed to fetch ModuleScript content")
		end
	end

	-- Run the installation
	installPackage()
end)

Here’s the repo:

It throws me an error “Base64Decode is not a valid member of HttpService ‘HttpService’”

2 Likes

No such method named “Base64Decode” is present in the HttpService class.
Refer this
You probably have to make your own functions to decode base64, or use existing modules like Base64 from this post.

1 Like

I used that Base64 module but the content is just Base64 still lol after running Decode(data.content) and setting the source. So the ModuleScript is being created but the ModuleScript is useless because the source is in Base64.

Also, it won’t load comments at all.

Nevermind. Got it working by using this Base64 Module by @Ozzypig

1 Like

I wrote a wrapper for HTTP service to add the method. I forgot I did that, I simply copied and pasted what I had, lol.

You will want to add the auth key if the repo is private. Assuming you are using proprietary software, you don’t want that info public.

Well, most of the packages will be added and maintained by other users.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.