Converting a Script from Server to Local

Hello, good day. I’m no scripter, and i’m having troubles how to turn this into a local script:

-- Core

-- Variables
local ButtonHandler = require(script.Functionality.ButtonHandler)

-- Functions
ButtonHandler:Init()

wait()
-- Button Tween
local TweenService = game:GetService("TweenService")
local ButtonStages = {
	DefaultColor = Color3.fromRGB(138, 255, 146),
	PressedColor = Color3.fromRGB(0, 0, 0)
}
local ButtonTweenInfo = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

-- Variables
local ButtonActions = {}

-- Function
local function GetButtonByID(id)
	for _, child in pairs (game.Workspace.Game.Buttons:GetChildren()) do
		if child.ID.Value == id then
			return child
		end
	end
end

function ButtonActions:Run(id)
	local button = GetButtonByID(id)
	local ButtonTween = TweenService:Create(button.Button, ButtonTweenInfo, {Position = Vector3.new(button.Button.Position.X, button.Button.Position.Y - .3, button.Button.Position.Z), Color = ButtonStages.PressedColor})
	ButtonTween:Play()
	require(script[id]):Execute()
	return
end

-- Return
return ButtonActions
local ButtonActions = require(script.Parent.ButtonActions)

local Buttons = {}
local ButtonHandler = {}

local function FindButton(id)
	local this = nil
	for _, child in next, workspace.Game.Buttons:GetChildren() do
		if child.Name == "ButtonModel" and child.ID.Value == id then
			this = child
		end
	end	
	return this
end

function ButtonHandler:Init()
	for _, child in next, workspace.Game.Buttons:GetChildren() do
		if child.Name == "ButtonModel" then
			table.insert(Buttons, child.ID.Value, false)
		end
	end
	return true
end

for _, child in next, workspace.Game.Buttons:GetChildren() do
	child.Button.Touched:Connect(function()
		if Buttons[child.ID.Value] == true then return end
		Buttons[child.ID.Value] = true
		ButtonActions:Run(child.ID.Value)
	end)
end

return ButtonHandler

This script is from [OS GAME] Push the Button with a little bit of adjustments in order to work the way i want it to work…

1 Like

this should work

-- LocalScript in StarterPlayer.StarterPlayerScripts

-- Core
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local ButtonHandler = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Functionality"):WaitForChild("ButtonHandler"))

-- Functions
ButtonHandler:Init()

--wait()
-- ButtonActions ModuleScripts

-- Button Tween
local TweenService = game:GetService("TweenService")
local ButtonStages = {
	DefaultColor = Color3.fromRGB(138, 255, 146),
	PressedColor = Color3.fromRGB(0, 0, 0)
}
local ButtonTweenInfo = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

-- Variables
local ButtonActions = {}
local ButtonsFolder = workspace:WaitForChild("Game"):WaitForChild("Buttons")

-- Function
local function GetButtonByID(id)
	for _, child in ipairs(ButtonsFolder:GetChildren()) do
		child:WaitForChild("ID")
		if child.ID.Value == id then
			return child
		end
	end
end

function ButtonActions:Run(id)
	local button = GetButtonByID(id)
	if button then
		button:WaitForChild("Button")
		local ButtonTween = TweenService:Create(button.Button, ButtonTweenInfo, {Position = button.Button.Position + Vector3.new(0, -.3, 0), Color = ButtonStages.PressedColor})
		ButtonTween:Play()
		require(script:WaitForChild(tostring(id))):Execute()
	end
end

-- Return
return ButtonActions
-- ButtonHandler ModuleScripts

local ButtonActions = require(script.Parent.ButtonActions)

local Buttons = {}
local ButtonHandler = {}

local buttonsFolder: Folder = workspace:WaitForChild("Game"):WaitForChild("Buttons")

local function FindButton(id)
	for _, child in ipairs(buttonsFolder:GetChildren()) do
		if child.Name == "ButtonModel" then
			child:WaitForChild("ID")
			if child.ID.Value == id then
				return child
			end
		end
	end	
end

function ButtonHandler:Init()
	for _, child in ipairs(buttonsFolder:GetChildren()) do
		if child.Name == "ButtonModel" then
			child:WaitForChild("ID")
			--table.insert(Buttons, child.ID.Value, false) -- this is a bit strange in this case
			Buttons[child.ID.Value] = false -- this is more suitable
		end
	end
--	return true
end

local OnChildAdded = function (child)
	child:WaitForChild("Button")
	child:WaitForChild("ID")
	child.Button.Touched:Connect(function ()
		if Buttons[child.ID.Value] == true then return end
		Buttons[child.ID.Value] = true
		ButtonActions:Run(child.ID.Value)
	end)
end

buttonsFolder.ChildAdded:Connect(OnChildAdded)
for _, child in ipairs(buttonsFolder:GetChildren()) do
	OnChildAdded(child)
end

return ButtonHandler

It is recommended to put the ModuleScripts in ReplicatedStorage.
image

4 Likes

Oh, thank you… Although, this pops up in Output:
21:04:25.570 - Players.agentpizza47.PlayerScripts.Core:7: attempt to call a nil value

1 Like

It means it’s trying to call something that doesn’t exist, hence nil

2 Likes

If you put the ModuleScripts as in the image everything should be fine. Could you put an image of where your scripts are after you have made the changes?
Also make sure you have copied the last line, where it says return, from the ModuleScripts

2 Likes

I just realized that I didn’t put the scripts in the same order that you published them. You probably copied the code into the wrong ModuleScript. Please check that.

1 Like

Oof…, that’s the problem. It’s working perfectly fine now. Thank you for all of your help, how can i ever repay you?