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
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
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
.