Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I get a LocalPlayer by touching a brick?

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!

1 comment

0

You should probably edit your title.

1 answer

✓ Accepted answer

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

6 comments

0

Can you please fix your answer so I can read it?

0

It isnt reading my code correctly. Trying to fix it.

0

"Part" has a blue underline and I'm unsure what to put for the connection line.

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.

0

I only put 'Part' because I was unsure as to where he had the script.

0

Thanks it works now!

This discussion is preserved as it was. New answers, comments, and votes are closed.