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

How do you give a value to a parameter when used in a function?

Asked by 8 years ago

I kinda understand how to do this but I need a better explanation. Also when I use the Touched event, I don't know how the parameter is defined like if the parameter is supposed to be a character, how is that defined?

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

The parameters take on the values of the arguments passed to the function when it is called.

For instance:

function printSomething(a) --the parameter 'a'
    print(a)
end

print('something') --the argument passed into 'a' is 'somethign'

In the case of ROBLOX Events, however, the passed in arguments are chosen by ROBLOX, not by you, so you have to use what it gives you.

function onTouch(hit)
    print(hit.Name)
end
script.Parent.Touched:connect(onTouch)

The Touched event provides one argument: the Part that 'touched' the Part the event itself is connected to.

To determine if that Part belongs to a Character model, it's common to check if hit.Parent:FindFirstChild("Humanoid") exists, but I prefer this:

function onTouch(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player then
        print("HEY I GOT TOUCHED BY A CHARACTER MODEL")
    end
end
script.Parent.Touched:connect(onTouch)

Doing this gives you the Player if you need it, and also works 100% of the time, and has no false positives if you use Zombies with Humanoids named "Humanoid".

0
What does GetPlayerFromCharacter mean? ComedyPumpkin 66 — 8y
0
http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter It's a method of game.Players, as seen in my code, that gets the Player object associated with the given Model, or `nil` if there isn't one. adark 5487 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

To pass an argument to a function, place the variable in the space respective to the argument you want it to count for. for example:

local a = 1
local b = 2

local function count(one,two) --the parameters we created are one and two
print(one,two)--print the arguments passed to the parameters
end

count(a,b) --prints 1 2
 --pass a and b to the function, in order. If we were to reverse the order, we would print 2 1

When using an event, the value returned by the event is passed as an argument to the first parameter of the given function. Different events return different values. The touched event returns whatever touched the given object.

local part = Instance.new("Part",workspace) --create part
part.Touched:connect(function(touched)--when part is touched, run the given code, and whatever touched the part is referenced by "touched"
print(touched)
end)

the parameter of the touched event can't actually be a character, because the character is a model and isn't corporeal. You can, however, find the character by checking the part's parent. If it's an arm or a leg, then it's parent will be the character.

local part = Instance.new("Part",workspace) --create part
part.Touched:connect(function(touched)--when part is touched, run the given code, and whatever touched the part is referenced by "touched"
if game.Players:GetPlayerFromCharacter(touched.Parent) then --checks to see if the part's parent is a player
print(touched.Parent) --if so, print the character that touched the part
end
end)

Answer this question