Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Different ways of making a speed boost?

Whats the difference between writing code like this -

local speedBoost = script.Parent

local function steppedOn(part)
	local parent = part.Parent
	if game.Players:GetPlayerFromCharacter(parent) then
		parent.Humanoid.WalkSpeed = 64
		
	end
end

speedBoost.Touched:connect(steppedOn)

or writing code like this -

script.Parent.Touched:connect(function(part)
    if not part.Parent then return end
 
    local h = part.Parent:FindFirstChild("Humanoid")
    if not h then return end
 
    h.WalkSpeed = 16 * 5
end)

I know the first style is function form, because it has 'Local Function' but is there a major difference? they both have the same exact output, increasing your speed by

1 answer

Essentially, one is defining the function to connect to the event inside of the connection statement.. the other isn't.

Btw - connect is deprecated. Use Connect.

it is

SomeObject.SomeEvent:Connect(function()
	--code
end)

vs

function Predefined()
	--code
end

SomeObject.SomeEvent:Connect(Predefined)

They do the same thing - I guess the first is useful if you need to connect the function to multiple events, but personally I prefer the second for how concise it is :)

The only other difference is how the function determines if it was a player that touched or not, and how it sets the property.

This discussion is preserved as it was. New answers, comments, and votes are closed.