Something wrong with this if statement

Code:

local eToContinueGui = game.ReplicatedStorage.UI.BillboardGui
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local shown = false
local epressed = false

local dialogueText = script.Parent.Frame.Frame.TextLabel
local ImageLabel = script.Parent.Frame.Frame.ImageLabel

local pictures = {
	"rbxassetid://5794645261", --Onyiistarr
	"rbxassetid://5781715416" --It's me no name
}

local ItsMeNoNamedialogues = {
	
	"Ayy Dude! It's almost Halloween! Let's celebrate!",
	"Feel Free to Visit anytime :D",
	"Did you know that there is gonna be a full Halloween Update (Event) by the end of October?",
	"It's kinda spooky out here, maybe playing some obbies will help set up the mood"
}

local OnyiiDialogues = {
	"Hey, Happy Halloween mate! Wanna buy some costumes, go to my homestore, link is in the description in the game :D",
	"Hi again, Did you know Im a growing youtuber, Can you sub to me mate? My channel is OnyiiStarr, Thanks Mate :D",
	"Hello"
}

local dialogueTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Elastic, Enum.EasingDirection.InOut, 0, false, 0)
local dialogueframe = script.Parent.Frame

local openTweenService = TweenService:Create(dialogueframe, dialogueTweenInfo, {Position = UDim2.new(0.25, 0,0.781, 0)})
local closedTweenService = TweenService:Create(dialogueframe, dialogueTweenInfo, {Position = UDim2.new(0.25, 0, 1.1, 0)})


print("inputed E")

local closestNPC,gui,currentNPC

local character = players.LocalPlayer.Character

userInputService.InputBegan:Connect(function(input)
	print(closestNPC)
	if input.KeyCode == Enum.KeyCode.E then
		print("Closest NPC")
		if epressed == false then
			print("e")
			if closestNPC == "ItsMeNoName" then
				print('bbruh')
				epressed = true
				shown = true
				print("E")
				local ItsMedialogue = ItsMeNoNamedialogues[math.random(1, #ItsMeNoNamedialogues)]
				dialogueText.Text = ItsMedialogue
				openTweenService:Play()
				wait(5)
				shown = false
				closedTweenService:Play()
				wait(1)
				epressed = false
			end
			
			if closestNPC == "Onyiistarr" then
				epressed = true
				shown = true
				local Onyidialogue = OnyiiDialogues[math.random(1, #OnyiiDialogues)]
				dialogueText.Text = Onyidialogue
				openTweenService:Play()
				wait(5)
				shown = false
				closedTweenService:Play()
				wait(1)
				epressed = false
			end
		end
		
	end
end)


local toggleGui = function(toggle)
	if gui then
		gui:Destroy()
		gui = nil
	end
	
	if toggle == true then
		gui = eToContinueGui:Clone()
		gui.Parent = closestNPC:FindFirstChild("EPart")
	end
end

runService.RenderStepped:Connect(function()
	local maxDistance = 15
	local newClosestNPC
	
	for _, NPC in pairs(game.Workspace.NPC:GetChildren())do
		if (NPC.EPart.Position - character.HumanoidRootPart.Position).magnitude <= maxDistance then
			maxDistance = (NPC.EPart.Position - character.HumanoidRootPart.Position).magnitude
			newClosestNPC = NPC
		end
	end
	
	closestNPC = newClosestNPC

	
	if closestNPC then
		toggleGui(true)
		return closestNPC
	else
		toggleGui(false)
	end
end)

In this I wanted to achieve displaying the proper UI whenever the player talks to a NPC but my problem is that the if statement on closestNpc (line 51 and 66) is somehow not working, so to solve the problem I added a print system on every statement to see if it’s really going trough it, in the output it says:
image
this happened ofc whenever I go to the desired NPC, it says here the Closes NPC value is ItsMeNoName but the if statement is somehow not working, there no errors in the output

All Help is appreciated, thank you :smiley:

Try adding breakpoints and debugging, there may be spaces or new line before or after.

What do you mean? I think I already did that because all of the prints I made in every new space of code has a print so I can debug it but the if statement on line 51 and 66 doesn’t seem to printing so that must be the source of the error

You can see the contents of the variable by debugging.

I already did it, as you can see I explained that I printed the variable Value and it’s printing out the exact same string as the string I put in the if statement.

Try copying and pasting it into Notepad, it may contain line breaks, spaces, etc.

So after 20 min of expirementing with the code, I tried using the

toString()

function, and it really worked! it seems that the variable value isn’t yet a string value.

If I am right, it is not a string value because in the nearest character loop, you are assigning the closestNPC to the nearest NPC’s instance not the Name of the NPC.

Hopefully this fixes it without the toString function being used:

runService.RenderStepped:Connect(function()
	local maxDistance = 15
	local newClosestNPC
	
	for _, NPC in pairs(game.Workspace.NPC:GetChildren())do
		if (NPC.EPart.Position - character.HumanoidRootPart.Position).magnitude <= maxDistance then
			maxDistance = (NPC.EPart.Position - character.HumanoidRootPart.Position).magnitude
			newClosestNPC = NPC
		end
	end
	
	closestNPC = newClosestNPC.Name --This is where I added .Name
	
	
	if closestNPC then
		toggleGui(true)
		return closestNPC
	else
		toggleGui(false)
	end
end)
1 Like

Thank you for that information, This really could help me with if statements in the future, Now I know :sweat_smile: :sweat_smile: :sweat_smile:

It isn’t really something that is just for usage with if statements, what I meant to say is you were checking if that instance is equal to a piece of string. Which clearly isn’t, so you just need to assign the variable with the Name of that instance, as that is what you are checking.

But it works without the toString function too, you just need to do .Name after the variable, so it references to its Name.

1 Like