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

Nooby question: How to target player?

Asked by
emite1000 335 Moderation Voter
10 years ago

I want to make an if statement that goes something along the lines of "if brick is a player, then do..." but I don't know how to differentiate between a normal brick and a character. Any help?

2 answers

Log in to vote
2
Answered by
duckwit 1404 Moderation Voter
10 years ago

In ROBLOX a Character is actually a Model - a collection of specific Instances such as Part, Humanoid, etc...

There are multiple ways to identify whether any given Model is a Player's Character, but the 2 most popular are:

  1. Seeing if the Name of the Model is one of the children of game.Players, as in if game.Players[model.Name] then (jav2612's answer)

  2. Seeing if the Model contains a Humanoid called "Humanoid", as in if model:FindFirstChild("Humanoid") then

Checking if a specific Part is a Player is slightly different because you need to check whether the Model that it belongs to is a Player's Character, rather than the Model itself - but this is easy to do with the Parent property!

You could also write this as a function so that you can easily check if a brick is a constituent part of a Player's Character from elsewhere in the script:

function IsPlayer(part)
    if part.Parent:FindFirstChild("Humanoid") then return true end
    return false
end

--You might use it like this
if IsPlayer(someRandomPart) then 
    --someRandomPart belongs to a player!
end
1
This would only tell if the model has a humanoid.... Lots of models use humanoids but dont have players. You will need to check Players to see if a player is associated with the character. jav2612 180 — 10y
1
I suggested that as a possibility as well, I only demonstrated one of them in code because your answer covered the other option. duckwit 1404 — 10y
1
When I tried to use the IsPlayer function it returned the error "attempt to call global 'IsPlayer' (a nil value)". emite1000 335 — 10y
2
You need to have the definition of the function in the script, it's not part of the roblox API. duckwit 1404 — 10y
0
Oh, I see. This is my first time creating a custom function. When I add the definition it now works. Thanks! emite1000 335 — 10y
Ad
Log in to vote
0
Answered by
jav2612 180
10 years ago
brick = TheBrick
players = game:GetService("Players")

if players:FindFirstChild(brick.Parent.Name) ~= nil then
    print("Le brick is a player.")
else
    print("Its just a brick....")
end


0
if your using a Touched event then your going to need to do brick.Parent.Name to get the name of the character. jav2612 180 — 10y
0
A Player's character will not be a Brick (Part) unless the OP is using custom characters, which I don't think he is. You need to use brick.Parent.Name instead, I do believe. duckwit 1404 — 10y
0
Thats what I just said.... jav2612 180 — 10y
0
I edited it so it should work now. jav2612 180 — 10y
View all comments (7 more)
0
I'm not sure I understand Line 4 of your script. I pasted this script into TheBrick along with the Touched event, but every time I touched it with my character it returned "Its just a brick". emite1000 335 — 10y
0
You have to define "TheBrick".... Obviously you cant just leave it the way it is. As for line 4, basically it checks to see if the character has a player associated with them. jav2612 180 — 10y
0
Yes, I already did replace "TheBrick" with the name of my brick (duh :P). So Line 4 is saying "if you find a child in 'players' not named 'Workspace', then print #1, but if you do find it, print #2? emite1000 335 — 10y
0
Actually, what does ~= mean? I know = and == but can't remember ~=. emite1000 335 — 10y
0
~= means not equal to. This script checks to see if a player with the same name as the brick's parent is not equal to nil, which means it exists. When a part of your character touches it you have to find the part's parent's name which is the characters name. Thats why you do part.Parent. jav2612 180 — 10y
0
But the part's parent is Workspace, so what you are asking is if a player has the same name as Workspace? emite1000 335 — 10y
0
No.... The parent of a torso or head or arms/legs are the character's Model. It takes the name of the character and looks for the player with the same name. jav2612 180 — 10y

Answer this question