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

How do I get the name of the player who touched a block?

I am completely confused. I put a '?' in the spot where I don't know what to put.

function claim()
	script.Parent.Parent.OwnerName.SurfaceGui.Owner.Text = "Owned by: "..?.."" -- What do I put where the '?' is?
end

script.Parent.Touched:connect(claim)

1 answer

✓ Accepted answer

Try this

script.Parent.Touched:connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		script.Parent.Parent.OwnerName.SurfaceGui.Owner.text = "Owned by: "..hit.Parent.Name
	end
end)

Here we:

  • Define the function in the event, to save space.
  • Make the argument hit inside the function, with the Touched event the only argument is the thing that touched it.
  • Check to make sure the parent of what touched it is a Player
  • If the thing that touched it is a child of a Player, then it makes the text have that name in it

If this helped remember to make it the correct answer, thank you, and you're welcome. :)

3 comments

1

That worked perfectly except you had 1 capitalization issue. The 'text' in string 3 need's to be capitalized, just for future reference.

0

Capitalization error.. line 3

0

Sorry about that, everything else was fine so that's cool.

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