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

what does the hit mean in the parentheses of that function?->function onTouched(hit)

Asked by
lytew 99
4 years ago

wanted to understand the use of it to try to find out which player did the event so that I can create a text that appears on the correct screen) in short, I want to understand the use of 'hit' script:

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(100)
    end
end

script.Parent.Touched:connect(onTouched)

this script can find out which player played a part so that it kills it ... and not another player that didn't even come close. I wanted to understand how

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hi! Today I fo to show you how to find a character with the Players Service is really easy!!

local PlayerService = game:GetService('Players') -- Players in explorer

script.Parent.Touched:Connect(function(hit) -- Yes you can make directly the function on the connection
    local find_player = PlayerService:GetPlayerFromCharacter(hit.Parent) -- Find the player
    if find_player and find_player.Character and find_player.Character:FindFirstChild('Humanoid') then -- Check if the player exist
        find_player.Character.Humanoid:TakeDamage(100)
    end
end)

To learn more about GetPlayerFromCharacter

0
interesting,but is it really necessary to put 3 conditions in the if like you did? lytew 99 — 4y
0
No, I wouldn't Spjureeedd 385 — 4y
0
I really tested it and it works, but there are many errors ... after I put the 3 conditions the errors disappeared and everything worked correctly ... I just wanted to understand how these three conditions work? lytew 99 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

That is the parameter passed by the .Touched event which refers to the part that touched the part you're listening for the event on. For example, say you have 2 parts in the game, part1 and part2:

function onTouched(hit)
    print(hit.Name)
end

part1.Touched:Connect(onTouched)

When part2 touches part1, it will fire the function and hit will be replaced with part2.

Answer this question