Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does OnServerEvent require a local function?

Asked by 4 years ago
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"?

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. jamespringles 105 — 4y
0
that also declares what it does, if you connect those two they will answer your question of why its needed moo1210 587 — 4y
0
Simple answer: It doesn't require a local function. This is just an issue with your code in general. DeceptiveCaster 3761 — 4y

1 answer

Log in to vote
0
Answered by
TickoGrey 116
4 years ago

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!

Ad

Answer this question