Script doesn't continue past "label.Visible = true"

  1. What do you want to achieve? Keep it simple and clear!
    I want to make so the local script removes the text that is created under the players mouse after looking away, the game is in first person.

  2. What is the issue? Include screenshots / videos if possible!
    I have managed to get the script to detect if the object that the mouse is pointing towards has a string value and a tag called “ForText” signed to it and also added a max activation distance using magnitude that checks the parts distance and humanoid distance. However I think my issue is that the script does not get past “label Visible = true” how do I know that is the issue? Because I have added two prints, one before the “else” statement and one after the “else” statement.

When this issue happens it doesn’t disable the text that got visible after the player looks away from the part, the text is supposed to only be visible when the player is looking at the part.

Example of issue:
image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried moving the “end” statement but I’m not sure if I have done correctly or if that even is the key to fixing the issue
-- local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local label = player.PlayerGui.MouseHoverText.TextLabel
local Thepart = game.Workspace.thehmm.hmm

local character = player.Character or player.CharacterAdded:wait()
local HumanoidRootPart = character:WaitForChild('HumanoidRootPart')

local CollectionService = game:GetService("CollectionService")

function updatePos()
	local workDistance = 6
	local target = mouse.Target

	if (Thepart.Position - HumanoidRootPart.Position).magnitude < workDistance then--magnitude checks the player distance
		if CollectionService:HasTag(mouse.Target, "ForText") then ---Checks if the object is asigned to any tags
			if target and target:FindFirstChild("MousehoverText") then ---Checks if the object has MousehoverText
				label.Position = UDim2.fromOffset(mouse.X, mouse.Y) ---puts the Textlabel under the mouse
				label.Text = target.MousehoverText.Value
				label.Visible = true ------does not get past this line
				print("test1") 
			else
				label.Visible = false
				print("test2")
			end

		elseif (Thepart.Position - HumanoidRootPart.Position).magnitude > workDistance then
			label.Visible = false

		end
	end
end

mouse.Move:Connect(updatePos)

updatePos()
4 Likes

I can provide with more information if needed.

1 Like

where is the local script parented?

4 Likes

StarterCharacterScripts
image

3 Likes

does it print if you put the statement before label.Visible = true?

2 Likes

I tried that now, yes it does.

2 Likes

is there any errors? what if you changed the elseif to just if?

2 Likes

When replacing “Elseif” with “if”


This is the error code that it gives

1 Like

I don’t see and problems with the code, the only other thing that I can think of is if you have the update for Roblox Studio that was released today, and if that doesn’t help then this might be a bug

2 Likes

I will update now,
here is a more clear image

2 Likes

After updating it is still the same problem and error code.

1 Like

When you change elseif to if, the if statement the elseif was on has to return then end before the new if statement.

2 Likes

Is this what you mean at line 17?
if not, is it possible if I could get and example?

1 Like

I’m referring to this:

Look at these 2 functions:

local value = true


function printval1()
	if value == true then
		print("yes")
		return
	end
	if value == false then
		print("no")
	end
end

function printval2()
	if value == true then
		print("yes")
	elseif value == false then
		print("no")
	end
end

The thing is that both functions work the same.

1 Like

One question, with the regular code, does anything pop up in the output?

1 Like

Nope nothing pops up in the output on regular code

1 Like

Seems like you accidently added a comment on the first line.

local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local label = player.PlayerGui.MouseHoverText.TextLabel
local Thepart = game.Workspace.thehmm.hmm

local character = player.Character or player.CharacterAdded:wait()
local HumanoidRootPart = character:WaitForChild('HumanoidRootPart')

local CollectionService = game:GetService("CollectionService")

function updatePos()
	local workDistance = 6
	local target = mouse.Target

	if (Thepart.Position - HumanoidRootPart.Position).magnitude < workDistance then--magnitude checks the player distance
		if CollectionService:HasTag(mouse.Target, "ForText") then ---Checks if the object is asigned to any tags
			if target and target:FindFirstChild("MousehoverText") then ---Checks if the object has MousehoverText
				label.Position = UDim2.fromOffset(mouse.X, mouse.Y) ---puts the Textlabel under the mouse
				label.Text = target.MousehoverText.Value
				label.Visible = true ------does not get past this line
				print("test1") 
			else
				label.Visible = false
				print("test2")
			end

		elseif (Thepart.Position - HumanoidRootPart.Position).magnitude > workDistance then
			label.Visible = false

		end
	end
end

mouse.Move:Connect(updatePos)

updatePos()
1 Like

:laughing: yes, but it isn’t in studio, I made a mistake while making the topic.

1 Like

Okay, I get it now, I tried still the same problem.

1 Like

First, I am going to say how I understand your code inside the updatePos() function.

The condition inside the first if statement is checking if the distance between Thepart and HumanoidRootPart is less than workDistance (or 6). But there is no else/elseif statement connected to this if statement, so nothing will happen if the distance is more than or equal to workDistance.

The condition inside the second if statement (the first nested if statement; the one inside the first if statement) will be met if target (mouse.Target) has a ForText tag. But if target doesn’t have the tag (the condition is not met), the elseif statement connected to this second if statement will check if the distance mentioned earlier is more than workDistance. What happens if the distance is less than or equal to workDistance? No actions will be taken.

And the condition of the last if statement will be met if, first, target isn’t nil (which can’t be the case since the if statement this if statement is nested in checks if target has a tag) and, second, if target has a child named "MousehoverText". If the condition is met, label’s Position and Text properties will be configured, and its Visible property will be set to true. However, if not, label will be invisible–its Visible property will be set to false.


So, the elseif statement connected to the second if statement needs to be connected to the first if statement instead. And since we’ll be checking if the distance between Thepart and HumanoidRootPart is <= workDistance (you should check if the distance is less than or equal to workDistance), the only other possible condition would be if currentDistance (the distance mentioned) is > workDistance. This means you can just use an else statement, rather than an elseif statement. Remember: you should use an elseif statement when there are multiple other possible conditions and it matters which of those conditions get met; otherwise, use an else statement.

-- If you have the player (Players.LocalPlayer), you can
-- use Player:GetDistanceFromCharacter(Thepart) as a replacement.
local currentDistance = (Thepart.Position - HumanoidRootPart.Position).Magnitude

if currentDistance <= workDistance then
    -- ...
else
    label.Visible = false
end

You should also have an else statement connected to the second if statement to make sure label.Visible = false executes when target does not have a tag:

if ... then
    if CollectionService:HasTag(target, "ForText") then
        -- ...
    else
        label.Visible = false
    end
else
    -- ... 
end

And I wonder this: if an object in your game has the ForText tag, does that mean the object has a child named "MousehoverText"? Because, if so, you can get rid of the last if statement. And checking if target exists does not seem to be necessary since the second if statement checks if target has a tag (if target didn’t have a tag, it would be nil or not an important-enough object necessary for the code to reach the next (last) if statement).


To prevent the nesting, you can use a method called Inversion. I don’t have much time right now, so here’s a quick example:

-- Instead of this ...
if blah1 > blah2 then
    if blah3 < blah4 then
        if blah5 == blah6 then
            doBlah()
        end
    end
end

-- Do this:
if blah1 <= blah2 then
    return
end

if blah3 >= blah4 then
    return
end

if blah5 ~= blah6 then
    return
end

doBlah()

And here’s how you’d apply that to your code:

if currentDistance > workDistance then
    label.Visible = false
    return
end

if not CollectionService:HasTag(target, "ForText") then
    label.Visible = false
    return
end

-- If needed, you can invert the last condition, too:
--[[
if not target or not target:FindFirstChild("MousehoverText") then
    label.Visible = false
    return
end
]]

-- (Configure label's properties)...
label.Visible = true

Sorry if this was sloppy, I had limited time. Have a perfect rest of your day.

1 Like