WT! Same value type, same value, but == is false?

I want to work out if the player touching the portal is player 1.

The issue is, I have the code saying everything is perfect.

I have print commands telling me it should be working.

So when a player joins, it stores them in the next available player slot.

P1Name.Value

That way I can access it anytime easily.

 Field.Touched:Connect(function(Hit)

 	print(type(Hit.Parent))
 	print(type(Control.P1Name.Value))

 	print(Hit.Parent)
 	print(Control.P1Name.Value)
 
 	print(Hit.Parent == Control.P1Name.Value)
 
 	if Hit.Parent == Control.P1Name.Value then
 		print("WORKING!!!")
 	end
 end)

It outputs this:

userdata
userdata
Steve_Speedy
Steve_Speedy
false

I’m serious. How is this possible?

What exactly is the problem?, From the code provided it seems that everything looks as expected( i’m saying in some cases it would be more useful to add more context on the situation)

1 Like

You’re probably accessing a character versus a player instance. Using rtype would show the class differences.

When referencing hit.parent, use game:GetService(“Players”):GetPlayerFromCharacter(Hit.Parent)

EDIT:

While reading your code, I instead suggest storing the player’s character and changing the value on character respawns to avoid issues with non-character models touching the field.

That would be why he’s here-because he has a problem and can’t figure it out.

1 Like

I agree mostly with @snorebear’s Answer , but also if you are doing some type of teleport system/portal or even if you are not and you are just using the touched event for something, i would recommend adding a Debounce to ensure the touched event is being called more than necessary!

1 Like

rtype doesn’t exist, unless you’re referencing an external library. Did you mean typeof? typeof is type but it accounts for Roblox classes and datatypes (also classes): both would return Instance, not the ClassName, because they’re of the same type.

2 Likes

Well they are the same type, the same text, and they are not equal. I need it to ask “Who is touching me!!”

And the character says “Steve_Speedy”

“Ok Mr controller, Who is player #1?”

And the controller says “Steve_Speedy”

“Ok then so Since Steve_Speedy is player #1, I’ll send him to the portal in the player #1 area.”

But the stupid thing says “Ok so since Steve_Speedy isn’t the same as Player #1 Steve_Speedy, I’m not sending him anywhere.”

It works fine with everything else. Just not this Portal.

typeof returns “instance” on both.

Now I tested it and added this:

	local temp1 = Hit.Parent
	local temp2 = Control.P1Name.Value
 	print(temp1.Character)
print(temp2.Character)

I got an Error on temp1 saying that Character was not part of the model. temp2 didn’t get to fire. Swapping them around means that temp2 returned the Character. but then temp1 returned the error.

I printed “Hit.Parent.Parent” and it returned “Workspace”.

So Hit is not actually returning the player to allow me to access the Character value. So even though they are identical, they are different locations. .character isn’t in “hit” but it is in my value.

I would love to see an object structure of a player. That way I could see what I am picking up and navigate around.

In the game while it’s running, I open Steve_Speedy in the explorer but I can’t find or Humanoid or anything like that. So I don’t know where I am and where I need to tell it to go from Hit to get the Character.Parent.

What is Hit actually returning? Because it’s in Workspace but I can’t see it in explorer.

Starting to think about ditching it and making a button and use a clicker. But it doesn’t feel like a real portal then.

I do like that Debounce Idea! That rocks!

1 Like

I think i know the problem (maybe), Hit is returning an instance, meaning that you are compairing a string to a object, therefor changing if Hit.Parent == Control.P1Name.Value then to if Hit.Parent.Name == Control.P1Name.Value then should fix the problem

Alternatively you could just tostring Hit.Parent (tostring(Hit.Parent))

(Also there are many other ways of checking if hit is the specified player, but this should work for now)

4 Likes

Super close.

So Hit.Parent.Name
And:
local temp2 = Control.P1Name.Value
temp2.Parent.Name

they returned true!

Thank you!

Just gotta see if it all works now :smiley:

Your

appears to only be the name, not the object itself. You should be checking if the name matches, not the object.

hit.Parent.Name == Control.P1Name.Value

You can also save the player object as the value if you use an ObjectValue, then you can compare objects rather than names.

Edit: looks like I was beaten to the punch, awe shucks…

1 Like

Yep got it all working :smiley: Needed to get the name of my P1Name.Value too because it wasn’t a string value and was coming up as false when comparing if they were the same.

So just getting the name of both and comparing the names, was perfect. Both strings then so easily comparable.

Thanks a million guys :smiley:

1 Like