Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Image Label not becoming invisible?

This is my code

I had to change the while true do loop to renderstepped now the ImageLabel Won't become invisible

local inputservice = game:GetService("UserInputService")

inputservice.InputBegan:Connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Door in pairs(workspace.Doors:GetChildren()) do
				game:GetService("ReplicatedStorage").DoorToggle:FireServer()
			break
		end
	end
		end
end)



game:GetService("RunService").RenderStepped:Connect(function()
	for _,Door in pairs(workspace.Doors:GetChildren()) do
		local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).magnitude
		if Mag <= Door.Range.Value then
			local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(Door.Center.Position)
			script.Parent.ImageLabel.Visible = true
			script.Parent.ImageLabel.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
			break
		end
	end
	wait()
end)

1 answer

✓ Accepted answer

You're never making the ImageLabel invisislbe. Here's a template for how you should handle problems like these, where you need to test a condition for all objects in a table, and do X if an object met the condition, or Y if it didn't :

local IsFound = false

for Key, Value in pairs(ObjectTable) do
	if Condition(Value) then
		IsFound = true
		break
	end
end

print(IsFound)

We first assume that no values in the table met the condition, and if one happens to, set it to true. In your example, it'd be something more like this:

local IsInDoorRange = false

for Key, Value in pairs(ObjectTable) do
	if Condition(Value) then
		IsInDoorRange = true
		break
	end
end

ImageLabel.Visible = IsInRange

Additionally, you should return immediately if the user input is a game processed event (i.e., a user is typing in chat, so don't handle the input), and declare more meaningful variable names for your parameters, like so:

UserInputService.InputBegan:Connect(function(InputObject, GameProcessedEvent)
	if GameProcessedEvent then
		return
	end

	-- Code
end)

The wait() at the end of your RunService.RenderStepped event listener is pointless, so you can just remove it.

This discussion is preserved as it was. New answers, comments, and votes are closed.