Hey everyone, I’m trying to understand how parameters function as variables or placeholders in functions, take this examplee
local function inputService(input)
if input.KeyCode == Enum.KeyCode.E then
end
end
Input is one of the parameters in this right?. However, input doesn’t take any value at all before a function call, it looks like a sort of a blank/random name that was never mentioned or assigned to a value before for the forthcoming variable.
How does the function know input means E? How can I ensure that input performs as expected ? If there is any more help or explanation, PLEASE, please go ahead!
I understand that parameters can be changed when a function is called, like in this example:
local function updateName(name)
local theName = name
end
updateName(“cyber”)
In this function name is a parameter that can be changed when the function is called with different arguments, like "cyber"
But that’s now what i’m looking for, I think you pretty much understand what i’m trying to point. Thank you
uhhhh
think of it like lego bricks
the bricks won’t be defined but they’re different colors, so the values are basically the same, just looking different
functions simply use paramaters, to connect, but they won’t be locked in place
For this question, I would say internally within Roblox’s C code engine (idk).
There is code that sends a signal with this input variable.
An example of signal based behavior is with bindable events.
local ServerScriptService = game:GetService("ServerScriptService")
-- Get reference to bindable event instance
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- Fire bindable event
bindableEvent:Fire("Round started!")
--some other script
local ServerScriptService = game:GetService("ServerScriptService")
-- Get reference to bindable event instance
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- Connect anonymous function to event
bindableEvent.Event:Connect(function(data)
print(data) --> Round started!
end)
In the above case the script is defined the send a string saying the round is started and the other script receives it as a string.
You just gotta trust that Roblox tests their code will always send a Enum in the first parameter slot. Otherwise you’ll have to use if statements and type check this variable.
Thank you, I think I’m starting to grasp it a bit better now
However, in functions like this: ( The script was taken by someone else for an example )
local Storage = game:GetService("ReplicatedStorage")
local Purchase = Storage:FindFirstChild("PurchaseEvent")
local weapons = Storage:WaitForChild("weapons")
Purchase.OnServerEvent:Connect(function(player, weaponName)
local findInfo = weapons:FindFirstChild(weaponName)
if findInfo then
local price = findInfo.Price
local tool = findInfo:FindFirstChildWhichIsA("Tool")
if price and tool then
if player.leaderstats.Cash.Value >= price.Value then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price.Value
local clone = tool:Clone()
clone.Parent = player.Backpack
end
end
end
end)
In this example what exactly does weaponName represent? Also, I feel like findInfo might have blank names (like weponname).
Also, Your Lego analogy is quite helpful!! I understand that player acts like a Lego piece used to build, but how can I become more familiar with using these? I find understanding easier when reading, but applying it myself seems CHALLENGING. Sorry if I’m asking too much