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

Why does OnServerEvent require a local function?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"
 
local function onCreatePartFired(player, color, position)
	print(player.Name, "wants to create a part")
	local newPart = Instance.new("Part")
	newPart.BrickColor = color
	newPart.Position = position
	newPart.Parent = game.Workspace
end
 
createPartEvent.OnServerEvent:Connect(onCreatePartFired)

When use this even it needs to be "local function OnCreatePartFired" or else it won't work. I just know one works and the other doesn't. Can someone explain why it needs to be a local function and can't just be "function OnCreatePartFired"?

4 comments

0

That explains what declaring something to be local is. Which I understand, but what I don't understand is why onCreatPartFired needs to be local because if it wasn't it doesn't work.

0

that also declares what it does, if you connect those two they will answer your question of why its needed

0

Simple answer: It doesn't require a local function. This is just an issue with your code in general.

1 answer

if you are asking why things need to be local then I believe I can tell you what you are looking for, local ensures you are not trying to change something that is called function, it also is useful for variables when type in "hotdog" local makes it so that it appears as a variable rather than changing something called hotdog

hope that helps!

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