I want to compare the values of 2 different string values
I have a server code that compares 2 string values 1. A string value within a NPC model within workspace and 2. A String with the script (server script) I get no errors but the script does not operate as intended
when the both values match they continue with the occupantNumbers set up if they do not match it prints “Do not match” in the output
Script:
-- Listen for the ReachedWaypoint2Event
ReachedWaypoint2Event.Event:Connect(function(occupantNumber, currentRoom)
print("Received event")
print("currentRoom.Value: ", game.Workspace.CurrentNPC:FindFirstChildOfClass("Model").Data.currentRoom.Value)
print("BuildingName: ", script.currentRoom.Value)
-- Check if the currentRoom matches the BuildingName
if game.Workspace.CurrentNPC:FindFirstChildOfClass("Model").Data.currentRoom.Value == script.currentRoom.Value then
-- Check the occupant number and set the corresponding OccupantInValue to true
if occupantNumber == 1 then
B101Occupant1InValue.Value = true
print("Occupant 1 has entered the room.")
elseif occupantNumber == 2 then
B101Occupant2InValue.Value = true
print("Occupant 2 has entered the room.")
else
print("Dont match")
end
end
end)
No matter where I place the String that is not within the NPC it always prints “Don’t Match”
but when they are printed in the Output they both look the same.
Additional Details
The Npc is cloned from Replicated Storage to Workspace also
The parameters within the Event Listener is not relevant at the moment
No errors were in the output
Thanks for reading if you have any suggestions or solution, Please Let Me know
Hi! I have a feeling that the crux of the issue lies in how you structured your if else statement. Let us take an example:
if a == b then
print("a is equal to b")
elseif a ~= b then
print("a is not equal to b")
else
print("Hello!")
end
This is when you want to check if two values are matching, alternatively you can do a==b and have an else statement for cases they do not match. In your case you have:
if a == b then
if O == 1 then
print("O is 1")
elseif O == 2 then
print("O is 2")
else
print("Don't match")
end
end
In this case, “Don’t match” doesn’t relate to the a == b as this condition was satisfied in order to print “Don’t Match”. This means “Don’t Match” only prints if O ~= 1 or 2.
-- Listen for the ReachedWaypoint2Event
ReachedWaypoint2Event.Event:Connect(function(occupantNumber, currentRoom)
print("Received event")
print("currentRoom.Value: ", game.Workspace.CurrentNPC:FindFirstChildOfClass("Model").Data.currentRoom.Value)
print("BuildingName: ", script.currentRoom.Value)
-- Check if the currentRoom matches the BuildingName
if game.Workspace.CurrentNPC:FindFirstChildOfClass("Model").Data.currentRoom.Value == script.currentRoom.Value then
-- Check the occupant number and set the corresponding OccupantInValue to true
if occupantNumber == 1 then
B101Occupant1InValue.Value = true
print("Occupant 1 has entered the room.")
elseif occupantNumber == 2 then
B101Occupant2InValue.Value = true
print("Occupant 2 has entered the room.")
else
print("Occupant is neither 1 nor 2.")
end
else
print("Dont match")
end
end)
TLDR: If you want to check if two string do not match, make an else statement to your if statement which compares the string value.
But the thing is Roblox studio comparing string is very weird even when i editted the if statement to compare the string to its self it also returned in the output as Don’t Match my main goal is for the script to compare the values of both string values