How do I get the player name?
part = script.Parent Player = game.Players.LocalPlayer.humanoid --Get name here part.Touched:connect(function(hit) hit.Transparency = 1 local hint = Instance.new("Hint", game.Workspace) hint.Text = Player.."Has touched me!" wait(1) hint:remove() end)
In a server script you cant even get the player by using >game.Player.LocalPlayer >. you can see it by 'LocalPlayer', its local.
You can also add debounce for not creating like 1000000 of hints like here:
local part = script.Parent local debounce = false if debounce then return end debounce = true part.Touched:connect(function() --your code not spammed print("Not spammed") end) debounce = false
if you want to know more on debounce, you can go one roblox wiki here
For beign sure its a player touching the part you can add this little code like that:
if hit.Parent:FindFirstChild("Humanoid") then --body end
here's the you're code:
part = script.Parent local debounce = false part.Touched:connect(function(hit) if debounce then return end debounce = true if hit.Parent:FindFirstChild("Humanoid") then hit.Transparency = 1 local name = hit.Parent.Name local hint = Instance.new("Hint", workspace) hint.Text = name.." Has touched me!" wait(1) hint:remove() end debounce = false end)
Firstly, There is no humanoid
property of Player
. Instead, to get the player's humanoid, use Player.Character.Humanoid
.
Secondly, staying true to the original question, the name of the player is Player.Name
.
Lastly, I should mention that Hint
, however simple and loved they may be, are deprecated. You should look into GUIs instead. Of course, that's only for really serious development. For small projects, it's fine.
Hi... You are calling your player variable wrong...from a server script its Player.Character not Player.LocalPlayer. Also When you do the text it has to be Player.Name. Here is the script.
part = script.Parent Player = game.Players.Character.Humanoid --Get name here part.Touched:connect(function(hit) hit.Transparency = 1 local hint = Instance.new("Hint", game.Workspace) hint.Text = Player.Name.."Has touched me!" wait(1) hint:remove() end)
I cant test this right now but it should work hope this helped.