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

Need Help Printing Player Name After Collision?

Asked by 5 years ago

I need to print out the player's name in the console when they touch a brick with their character, but my code doesn't work. can anyone help me solve this issue?

--variables

local brick = script.Parent

--functions

local function startLevel(player)

    print(player.Name)

end

--code

brick.Touched:Connect(startLevel(otherPart))

1 answer

Log in to vote
0
Answered by
MiguRB 60
5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Here's an easier way:

local brick = script.Parent

function startLevel(hit)
    if hit.Parent then
        if hit.Parent:FindFirstChild("Humanoid") then
            print(hit.Parent.Name)
        end 
    end
end

brick.Touched:Connect(startLevel)

Here's how this works: Once the function is fired (when the brick is touched) it checks does the object that hit the brick still exist, if it does, then it checks does the object's Parent (Obviously the character) have a Child called "Humanoid", if it does, we can be sure the toucher was a player, and then print the player's/character's Name.

You did the variable defining correctly, but use this function instead.

0
What if "hit" gets destroyed? What if hit isn't a player object? This would print on everything that happens to touch the object Fifkee 2017 — 5y
0
Stop. Spoonfeeding. Period. DeceptiveCaster 3761 — 5y
0
Looks like I did it too quickly. Fixed anyways, sorry! MiguRB 60 — 5y
Ad

Answer this question