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

What am I doing wrong with functions?

Asked by 10 years ago

Strangely whenever I do coding (I am terrible) it seems to think functions are well, not functions like when I do this...

local brick = game.Workspace.Brick

function onTouch(part)
(something)

end

brick.Touched:connect(onTouch)

It always does this (or something like it)

function onTouch(part



)

(then rest of code from other example.)

And it won't work at all! Can somebody with experience with this reply and tell me whats wrong?

3 answers

Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
10 years ago

@alessandro112 A lot of that is inaccurate. Anonymous functions aren't only called once, you just can't reference them anywhere. They're called every time the event fires, assuming they're connected to an event. I don't see why you wouldn't use Players:GetChildren() rather than Players:GetPlayers() if you're only looking for Players anyway. Additionally, that script would not do what you expect it to do. A more logical way to do this would be to retrieve the Player through the other part that has touched this part. I would do something like this-

local Part = Workspace.PartName -- or script.Parent, etc. depending on your script placement
local Players = Game:GetService("Players") -- Get the Players service

Part.Touched:connect(function(foreignPart) -- anonymous function :o
    local plr = Players:GetPlayerFromCharacter(foreignPart.Parent)
    -- if plr is not nil, 'foreignPart' must be a child of a Character in which case a Character has touched the 'Part'
    if plr then
        local char = foreignPart.Parent
        char:BreakJoints() -- more efficient way to kill a character
    end
end)
0
Strangely I tried to remake this script to learn stuff and it did not work Grappler123 10 — 10y
0
Output? Ekkoh 635 — 10y
Ad
Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
10 years ago

I don't see what you are trying to do with this part:

function onTouch(part



)

You should just have:

brick.Touched:connect(function(part)
    --Code
end)
Log in to vote
0
Answered by 10 years ago

@huner2 This is an anonymous function. @QuestionCreator If you want an anonymous function (this means that can be called once per connections) then use what huner2 wrote. Otherwise just do what did you do but remove the "()", those aren't used in lua.

local part = game.Workspace.YourPartName
function onTouch(part)
--Printing on the output
    print("Hello!")
end

part.Touched:connect(onTouch)

If you want to do something to the player who touched the part then you need first to create a local script, then declare the LocalPlayer variable:

local player = game.Players.LocalPlayer

And then execute your function

function onTouch(part)
    player.Character.Humanoid.Health = 0
end

game.Workspace.YourNamePart.Touched:connect(onTouch)

If you want that to be called in the SERVER (Normal Script) and not in the CLIENT (Local Script) then you need to get the player who touches the part by doing this:

local players = game.Players:GetChildren() -- Don't use GetPlayers()
local player -- We will define this variable in a function

-- This function simply puts the player passed in our player variable, it will be called outside everything!
function getPlayer()
    game.Players.PlayedAdded:connect(function(p)
        player = p
    end
end

-- For each player that touches the part, it will be killed
function onTouch(part)
    player.Character.Humanoid.Health = 0
end

 -- We connect the onTouch function to the Touched event of your part =)
game.Workspace.YourNamePart.Touched:connect(onTouch)
-- Now we need to call our getPlayer function:
getPlayer()

Answer this question