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

How would I get a LocalPlayer by touching a brick?

Asked by 8 years ago

Yeah, title doesn't make sense a bit. So basically, I'm trying to make soemthing that takes the LocalPlayer who touched the part, and then finds what team they are on and prints that they are on the right/wrong team. I have this so far:

function touch(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent.TeamColor == BrickColor.new("Bright red")then
    print("right team")
    end
end
script.Parent.Touched:connect(touch)

Sorry If I messed up, but any help will be good! Thanks!

0
You should probably edit your title. Mr_Octree 101 — 8y

1 answer

Log in to vote
1
Answered by
Mr_Octree 101
8 years ago

Here is a tip that makes functions a lot less time consuming Instead of making

function touch(hit)
end

and having to connect the function at the end of the script,You can just do this

Part.Touched:connect(function(hit) --Part being the part that you want to be touched.
end)

To answer your question, you can just use GetPlayerFromCharacter()

Part.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and player.TeamColor == BrickColor.new("Bright red") then
        print("Right Team")
    else
        print("Wrong Team")
    end
end)

--EDIT: The text editor didn't seem to wont to cooperate with me :P

0
Can you please fix your answer so I can read it? PyccknnXakep 1225 — 8y
0
It isnt reading my code correctly. Trying to fix it. Mr_Octree 101 — 8y
0
"Part" has a blue underline and I'm unsure what to put for the connection line. PyccknnXakep 1225 — 8y
0
Part should be script.Parent, assuming the script is parented to the part you want to be touched. You should be checking if the player has a humanoid before getting the player and then checking to see if the player exists before getting their TeamColor. Spongocardo 1991 — 8y
View all comments (2 more)
0
I only put 'Part' because I was unsure as to where he had the script. Mr_Octree 101 — 8y
0
Thanks it works now! PyccknnXakep 1225 — 8y
Ad

Answer this question