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

How to display Player's name when touching the ball?

Asked by 8 years ago

When the player touches the ball, It will display his/her name. I don't know if I should use a local script or a regular script. I did this but it did not work. By the way, the text is a billboardGUI

local BallTarget = game.Workspace.BallTarget
local player = game.Players.LocalPlayer
local PlayersName = game.Workspace.BallTarget.BillboardGui.PlayersName

local BallTarget = game.Workspace.BallTarget

BallTarget.Touched:connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then

    BallTarget.PlayersName = hit.Parent:FindFirstChild("Name").Text

    end
end)

1 answer

Log in to vote
0
Answered by 8 years ago

A few things to note about your code:

• You've already define PlayersName, so just use PlayersName (instead of BallTarget.PlayersName). Also, you should be setting properties like Text like so: PlayersName.Text

• Assuming Name is a property, you only need to do .Name. Also, you don't need .Text after the name. You do need that for setting the text in PlayersName, however.

• You should include a check to see if the player is actually a player. You can do this by using GetPlayerFromCharacter.

• You defined BallTarget twice, you only need to define a variable once. Also, you can reference variables that are previously made in your script when setting other variables (assuming the referenced variable is global or in the same scope.)

• Finally, you should use this in a normal Script in the BallTarget brick/union so that the Touched event works correctly.

Following the advice above, your code should look like this:


local BallTarget = script.Parent --Put this script inside of the ball.
local PlayersName = BallTarget.BillboardGui.PlayersName

BallTarget.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then --Extra check to make sure the player is a player.
    PlayersName.Text = hit.Parent.Name --Set the text to the player's name.
    end
end)

I hope my answer helped you. If it did, be sure to accept it.

Ad

Answer this question