Should tools and remotes always be scripted in this format?

Hi, I’ve seen a lot of tool models written like this and I was wondering if this is really necessary for tools. If you know anything about it, please let me know!

local gun =  script.Parent
local re = script.Parent:WaitForChild("RemoteEvent")
local player = nil
local mouse =  nil
local connection =  nil

local function onActivated() --This line
	re:FireServer(mouse.Target)
end

local function onEquipped()
	player = game.Players.LocalPlayer
	mouse = player:GetMouse()
	connection = gun.Activated:Connect(onActivated) -- This line
end

local function onUnequipped()
	player = nil
	mouse = nil
	connection:Disconnect()
end

gun.Equipped:Connect(onEquipped)     --These lines
gun.Unequipped:Connect(onUnequipped) --These lines

wouldn’t it be better to do something like this? instead of a callback?

gun.Equipped:Connect(function()
   rjkiaebf
   wekfawiuefbiu
end)

I’ve also seen remote functions written this way, and specifically remember from a DevKing video he said it should be like this.

remote.OnServerInvoke = function()
    aefa
    EfRGFO
end)
1 Like

In regards to the tool code formatting, there isn’t any difference between using a callback function and creating one inside :Connect, personally I only use a callback when it needs to be repeated. It’s all up to preference and how you organize your code.

2 Likes

This is what I use in my tool scripts.

can be shortened as

local player, mouse, collection = nil

which can also be applied to the rest of the script, or wherever you are setting multiple variables to one value (also referred to as a multiple assignment). Lua syntax is pretty flexible.