"Is Equal To" check not working properly?

I’m trying to make a chair with a proximityprompt in it, that ejects the occupant from the chair as long as the person activating the prompt is not sitting in the chair at the same time. My code looks like this…

script.Parent.ProximityPrompt.Triggered:Connect(function(player) --whoever touches the button
	local chairperson = script.Parent.Occupant.Parent.Name
	print ("player")
	print ("chairperson")
	if player == chairperson then --whoever is sitting in the chair
		print ("no change")
	else
		script.Parent.SeatWeld:Destroy()
	end	
end)

but even if the two values are equal, the “else” statement ALWAYS fires.

What gives?

(the print statements on lines 3 and 4 are so that I can check that the value outputs are the same, and both the proximityprompt and script are parented to the same seat.)

Hey there!
The issue with your code is that you are comparing a player object with a string value.

The Problem

Specifically, in the line local chairperson = script.Parent.Occupant.Parent.Name , chairperson is a string representing the name of the parent of the Occupant object.

Later, when you compare player (which is a player object) with chairperson (which is a string), the comparison will always be false because you’re comparing different data types.

The Fix

To fix this, you should compare the player object with the Occupant object instead of the Occupant 's parent name. You can modify your code like this:

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
    local chairOccupant = script.Parent.Occupant.Value
    if player == chairOccupant then
        print("No change")
    else
        script.Parent.SeatWeld:Destroy()
    end
end)

In this modified code, chairOccupant is a reference to the Occupant object. Then, you can compare player directly with chairOccupant to check if they are the same object.

In your free time, feel free to look over Data Types.

1 Like

Occupant.Parent.Name is a string, player is a Player.
You can work around this by using player.name on the left side of the ==. That should suffice for now. Later on you might consider taking the time to learn to use GetPlayerFromCharacter as a more ‘proper’ comparison. Here’s some example code for that I found on the creator documentation website, but again don’t stress it. You don’t need to try to tackle this right now.

    local humanoid = seat.Occupant
	if humanoid then
		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			print(player.Name .. " has sat down")
			currentPlayer = player
			return
		end
	end

You’re comparing a Humanoid with a Player now. ChatGPT isn’t very good at programming, I’ve quickly come to realize. You could at least spend some time reviewing what it has to say instead of copying the response blindly. It’s bad advice and isn’t helpful.

I believe Occupant is actually the Humanoid of the character, as mentioned by JarodOfOrbiter.

You can also use their solution but you could also use:

if player.Character and player.Character:FindFirstChildWhichIsA("Humanoid") == script.Parent.Occupant then

chairperson is a string. It should be:

if player.Name == chairperson then

or:

--players is a service
--chairperson is the character
if players:GetPlayerFromCharacter(chairperson) == player then

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.